iOS UI08_TableView界面传值

实现两个界面之间内容的传递

//
//  MainViewController.m
//  UI08_TableView界面传值
//
//  Created by dllo on 15/8/7.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "MainViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate,SecondViewControllerDelegate>
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)UIImageView *imageView;
@end

@implementation MainViewController
-(void)dealloc
{
    [_arr release];
    [_imageView release];
    [_tableView release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent=NO;
    self.navigationItem.title[email protected]"表视图";
    self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.backgroundColor=[UIColor yellowColor];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.tableView.rowHeight=50;

    self.tableView.dataSource=self;
    self.tableView.delegate=self;

    self.imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"h1.jpeg"]];
    self.imageView.frame=CGRectMake(0, -200, self.view.frame.size.width, 200);
    //给tableview加入头视图
    //宽是tableview的宽度
//    self.tableView.tableHeaderView=self.imageView;
    [self.tableView addSubview:self.imageView];

    self.tableView.contentInset=UIEdgeInsetsMake(200, 0, 0, 0);

}
#pragma mark tableview的delegate已经签订好scrollerView的协议,仅仅要设置代理人,就能够使用scrollerview的协议方法
//仅仅要滑动就会触发
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
   //获取偏移量
    CGFloat y=scrollView.contentOffset.y;
    NSLog(@"%g",y);
    if (y<0) {
        self.imageView.frame=CGRectMake(0, y, self.view.frame.size.width, -y);
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *[email protected]"reuse";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
}
    cell.textLabel.text=self.arr[indexPath.row];
    cell.detailTextLabel.text=[NSString stringWithFormat:@"%ld",indexPath.section];
    cell.imageView.image=[UIImage imageNamed:@"天平.png"];
    return cell;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"水浒";
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.arr;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondViewController *secVC=[[SecondViewController alloc] init];
    secVC.name= self.arr[indexPath.row];

    [self.navigationController pushViewController:secVC animated:YES];
    [secVC release];
    //

    secVC.delegate=self;

}

-(void)changeValue:(NSString *)value
{
    //属性的数组,相当于数据源,把传过来的值加入到数组中
    [self.arr addObject:value];
    //对数据进行刷新
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
//
//  SecondViewController.h
//  UI08_TableView界面传值
//
//  Created by dllo on 15/8/7.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import <UIKit/UIKit.h>
@protocol SecondViewControllerDelegate <NSObject>

//协议方法
-(void)changeValue:(NSString *)value;

@end
@interface SecondViewController : UIViewController
@property(nonatomic,copy)NSString *name;

@property(nonatomic,assign)id<SecondViewControllerDelegate>delegate;

@end
//
//  SecondViewController.m
//  UI08_TableView界面传值
//
//  Created by dllo on 15/8/7.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()
@property(nonatomic,retain)UILabel *label;
@property(nonatomic,retain)UITextField *textfield;
@property(nonatomic,retain)UIButton *button;

@end

@implementation SecondViewController
-(void)dealloc
{
    [_label release];
    [_textfield release];
    [_button release];
    [_name release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor cyanColor];

    self.label=[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 150, 40)];
    self.label.backgroundColor=[UIColor redColor];
    self.label.layer.borderWidth=1;
    self.label.layer.cornerRadius=10;
    [self.view addSubview:self.label];
    //赋值
    self.label.text=self.name;
    [self.label release];

    self.textfield=[[UITextField alloc] initWithFrame:CGRectMake(100, 200, 150, 40)];
    self.textfield.layer.borderWidth=1;
    self.textfield.layer.cornerRadius=10;
    [self.view addSubview:self.textfield];
    [self.textfield release];

    self.button=[UIButton buttonWithType:UIButtonTypeSystem];
    self.button.frame=CGRectMake(200, 300, 150, 30);
    [self.button setTitle:@"返回" forState:UIControlStateNormal];
    [self.view addSubview:self.button];
    self.button.layer.borderWidth=1;
    self.button.layer.cornerRadius=5;
    [self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

}
-(void)click:(UIButton *)button
{
    [self.delegate changeValue:self.textfield.text];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
时间: 2024-07-30 10:19:52

iOS UI08_TableView界面传值的相关文章

iOS开发- 界面传值(1)-通知模式(广播)

之后的几篇博客, 记录下不同界面间传值的常用办法. 这篇文章记录广播的方式. iOS的设计模式中,通知模式也是其中重要的模式之一,Notification直译为通知,其实本人觉得叫做广播模式更为贴切.它的作用就是一个对象对多个对象的同步操作.用法很简单,一个对象发出一个广播,需要收听的听众就先注册一下,然后选定频道,完了就可以收听广播的内容了. 但是要注意一点, 在收听之前, 一定要先注册. 不然发送的广播接受不到, 也就是值改变了, 不会做出响应. 下面是一个简单的demo, 效果如下: 第一

ios学习(界面传值的方法)

ios(学习)界面传值的方法 block: 实现界面传值的方法1.block: 实现界面传值,都是从第二个界面向第一个界面传值:第一种block 首先).在第二个界面secondViewController声明set方法 声明block @property (nonatomic,copy) void(^change)(UIColor *color); 其次).在.m文件实现 实现block的方法 _callback([UIColor redColor]);//注意这里之所以是_callback的

iOS中block用法之两个界面传值问题

Block的使用有很多方面,其中传值只是它的一小部分,但是很常用更实用,下面介绍Block在两个界面之间的传值用法: 先说一下思想: 首先,创建两个视图控制器,在第一个视图控制器中创建一个Label和一个Button,其中Label是为了显示第二个视图控制器传过来的字符串, Button是为了push到第二个界面. 第二个界面的只创建一个TextField,是为了输入文字,当输入文字并且返回第一个界面的时候(第二个视图将要消失的时候),就将这个 TextFiled中的文字传给第一个界面并且显示在

iOS开发之不同界面传值

iOS开发中不可避免要用到不同界面传值,今天就总结下三种传值方法:闭包传值.代理传值和通知传值. 1.闭包传值 如图建立VC1和VC2,com+R运行正常~把VC2的button用Action关联并设置tag. 在VC2中定义一个闭包 var closures: ((clicked: NSString) -> Void)? 设置VC2中三个button的点击事件(这里三个button的tag设置为101,102,103) @IBAction func numBtn(sender: AnyObje

iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)

iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notification方式:3.block方式:4.UserDefault或者文件方式:5.单例模式方式:6.通过设置属性,实现页面间传值 在iOS开发中,我们经常会遇到页面间跳转传值的问题,现归纳总结一下: 情况1:A页面跳转到B页面 方法: 在B页面的控制器中,编写对应的属性,在A页面跳转到B页面的地方,给

iOS 页面间传值 之 单例传值 , block 传值

ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同一个对象,对象的属性相同,所以可以用来传值. block 传值 与 代理传值相似,主要用于第二个页面向第一个页面传值,block 传值具体步骤: 在第二个页面: 1.声明: block typedef void(^SendMessagerBlock) (NSString *str); 2.创建方法:

iOS:Swift界面实例1, 简单界面

Apple推出了基于Objective-C的新语言Swift. 通过实例, 我们可以很好的感受这门新语言 注意事项: 在XCode6_Beta中, 如果有中文, IDE的自动补全功能就会失效, 所以开始调试的时候可以先用英文, 后面再用中文替代. 1. 新建iOS -> Single View Application. 2. 修改AppDelegate.swift文件 1 // 2 // AppDelegate.swift 3 // UIByCode_Swift_1_HelloWorld 4 /

界面传值

属性传值 1.属性传值用于第一个界面向第二个界面传值 2.明确两者联系的桥梁,也就是触发跳转的地方 3.明确传输的值  类型是什么 4.在第二个视图控制器内部声明相对应类型的属性 来接收传输的值 5.在第二个界面使用传入的值 1.声明协议 UI中的协议为当前类名 + Delegate 2.  声明代理 3.代理对象执行协议 4.指定第二个界面的代理对象为自己 5.接收协议 6.实现协议中的方法

属性传值,协议传值,block传值,单例传值四种界面传值方式

一.属性传值 对于属性传值而言,相对于其它的三种 方法来说,是最基础,最简单的一种 方法,但,属性传值 有很大的局限性,因为是适用于第一个界面向第二个界面传 值,第二个向第三个界面传值等等.N界面向N + 1界面传值.而在此基础上,必须知道跳转界面的明确位置及所要传的值的具体类型.在第二个界面中声明所要传值 类型的属性. @interface SecondViewController : UIViewController //声明一个字符串属性来保存第一个界面传过来的字符串内容 @propert