UI5_UINavigation传值

//
//  AppDelegate.m
//  UI5_UINavigation传值
//
//  Created by zhangxueming on 15/7/7.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    ViewController *rootViewController = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    self.window.rootViewController  = nav;
    
    return YES;
}

 

//
//  ViewController.h
//  UI5_UINavigation传值
//
//  Created by zhangxueming on 15/7/7.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "SubViewController.h"

@interface ViewController : UIViewController <sendTitleReport>

@end


//
//  ViewController.m
//  UI5_UINavigation传值
//
//  Created by zhangxueming on 15/7/7.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "ViewController.h"
#import "SubViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor cyanColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(100, 200, self.view.frame.size.width-200, 50);
    [btn setTitle:@"push" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont boldSystemFontOfSize:24];
    [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    
    self.navigationItem.title = @"按钮一";
    
    
}

- (void)btnClicked
{
    NSLog(@"-------");
    SubViewController *svc = [[SubViewController alloc] init];
    //正向传值: 最简单的做法, 在子视图控制器中定义属性实例变量接受值
    svc.delegate = self;//设置代理
    svc.currentTitle = self.navigationItem.title;
    [self.navigationController pushViewController:svc animated:YES];
}

- (void)sendButtonTitle:(NSString *)title
{
    self.navigationItem.title = title;
}

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

@end

 

//
//  SubViewController.h
//  UI5_UINavigation传值
//
//  Created by zhangxueming on 15/7/7.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol sendTitleReport <NSObject>

- (void)sendButtonTitle:(NSString *)title;

@end



@interface SubViewController : UIViewController

//记录当前被选中按钮的标题
@property (nonatomic, copy)NSString *currentTitle;
//防止相互引用
@property (assign, nonatomic) id <sendTitleReport> delegate;

@end


//
//  SubViewController.m
//  UI5_UINavigation传值
//
//  Created by zhangxueming on 15/7/7.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import "SubViewController.h"

@interface SubViewController ()

@end

@implementation SubViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    NSArray *titles = @[@"按钮一",@"按钮二",@"按钮三"];
    for (int i= 0; i<3; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(100, 100+i*100, self.view.frame.size.width-200, 50);
        [btn setTitle:titles[i] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont boldSystemFontOfSize:24];
        [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 200+i;
        //设置选中状态
        if ([self.currentTitle isEqualToString:btn.currentTitle]) {
            btn.selected   = YES;
        }
        [self.view addSubview:btn];
    }
}

- (void)btnClicked:(UIButton *)btn
{
    if([self.delegate respondsToSelector:@selector(sendButtonTitle:)])
    {
        [self.delegate sendButtonTitle:btn.currentTitle];
    }
    [self.navigationController popViewControllerAnimated: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

 

posted @ 2015-07-11 15:28  sirzhang  阅读(226)  评论(0编辑  收藏  举报
AmazingCounters.com