UIViewController 正向,反向传值

来源:互联网 发布:域名不支持生成短网址 编辑:程序博客网 时间:2024/05/17 23:48

正向传值,通过属性来传值,反向传值通过协议代理。

#import <UIKit/UIKit.h>#import "SecondViewController.h"@interface RootViewController : UIViewController <SendValue>@end#import "RootViewController.h"#import "SecondViewController.h"@interface RootViewController ()@end@implementation RootViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view.    }- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (void)viewWillAppear:(BOOL)animated{    if (self.navigationController.toolbarHidden){        self.navigationController.toolbarHidden = NO;    }}- (void)loadView{    [super loadView];    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];    UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:nil];    UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];    NSArray *items = [NSArray arrayWithObjects:space, item, space, item2, space, nil];    [item release];    [item2 release];    [space release];    self.toolbarItems = items;        [self.view setBackgroundColor:[UIColor purpleColor]];    self.navigationItem.title = @"whwh";    //正向,反向传值    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];    [button1 setFrame:CGRectMake(100, 100, 80, 40)];    [button1 setTitle:@"push" forState:UIControlStateNormal];    [self.view addSubview:button1];    [button1 addTarget:self action:@selector(pushNav:) forControlEvents:UIControlEventTouchUpInside];}- (void)pushNav:(id)sender{    SecondViewController *sec = [[SecondViewController alloc]init];    sec.delegate = self;    self.navigationController.toolbarHidden = YES;    sec.modalTransitionStyle = UIModalTransitionStyleCoverVertical;    [self.navigationController pushViewController:sec animated:YES];    [sec release];}#pragma mark  sendValueDelegate- (void)sendBtnTitle:(NSString *)title{    self.navigationItem.title = title;}@end#import <UIKit/UIKit.h>@protocol SendValue <NSObject>- (void)sendBtnTitle:(NSString *)title;@end@interface SecondViewController : UIViewController@property (nonatomic, assign) id<SendValue> delegate;@property (nonatomic, copy) NSString *currentTitle;@end#import "SecondViewController.h"@interface SecondViewController ()@end@implementation SecondViewController@synthesize  delegate = _delegate;@synthesize currentTitle = _currentTitle;- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (void)loadView{    [super loadView];    //正向,反向传值          for (int i = 0; i < 3; i ++){        UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];        [button3 setFrame:CGRectMake(100, 200 + i * 50, 80, 40)];        [button3 setTitle:[NSString stringWithFormat:@"button %i", i] forState:UIControlStateNormal];        [button3 addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside];        [self.view addSubview:button3];     }}- (void)onclick:(UIButton *)btn{    NSString *title = btn.currentTitle;    if ([_delegate respondsToSelector:@selector(sendBtnTitle:)]){        [_delegate sendBtnTitle:title];    }    [self.navigationController popViewControllerAnimated:YES];}@end


原创粉丝点击