UITabBarController 标签视图控制(传值方式: 属性,代理,单例,block)

UITabBarController 标签视图控制器 (传值方式: 属性, 代理, 单例, block) 

    UINavigationController, 导航控制器, 用于管理具有层级关系的视图控制器 

    UINavigationBar, 导航栏, 只有一个, 多个视图控制器共用一个 

    UINavigationItem, 每一个视图控制器都有一个, leftBarButtonItem(s), title(titleView), rightBarButtonItem(s) 

    UITabBarController, 标签控制器, 继承于UIViewControlller, 用于管理具有平级关系的视图控制器

    UITabBarController *tabBC = [[UITabBarController alloc] init];
    tabBC.delegate = self;
    OneViewController *oneVC = [[OneViewController alloc] init];

   UITabBarItem,用于管理标签栏上的图片和文字的展示, 继承于UIBarItem

TabBarController.m
#import "TabBarController.h"
#import "HomeViewController.h"
#import "SearchViewController.h"
#import "CategoryViewController.h"
#import "SettingViewController.h"
@interface TabBarController ()
@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   被UINavigationController管理的视图控制器, 具有UINavigationItem
   被UITabBarController管理的视图控制器, 具有UITabBarItem
   
    //主页
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    UINavigationController *homeNavC = [[UINavigationController alloc] initWithRootViewController:homeVC];
    homeNavC.tabBarItem.title = @"主页";
    homeNavC.tabBarItem.image = [[UIImage imageNamed:@"Home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    homeNavC.tabBarItem.selectedImage = [UIImage imageNamed:@"Home1"] ;
   
    //搜索
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    UINavigationController *searchNavC =  [[UINavigationController alloc] initWithRootViewController:searchVC];
    searchNavC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:(UITabBarSystemItemSearch) tag:100];
    
    //分类
    CategoryViewController *categoryVC = [[CategoryViewController alloc] init];
    UINavigationController *categoryNavC = [[UINavigationController alloc] initWithRootViewController:categoryVC];
    categoryNavC.tabBarItem.title = @"分类";
    categoryNavC.tabBarItem.image = [[UIImage imageNamed:@"category"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    categoryNavC.tabBarItem.selectedImage = [UIImage imageNamed:@"category"] ;
    
    //设置
    SettingViewController *settingVC = [[SettingViewController alloc] init];
    UINavigationController *settingNavC = [[UINavigationController alloc] initWithRootViewController:settingVC];
    settingNavC.tabBarItem.title = @"设置";
    settingNavC.tabBarItem.image = [[UIImage imageNamed:@"setting2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    settingNavC.tabBarItem.selectedImage = [UIImage imageNamed:@"setting3"];
    //设置tabBarController管理的视图控制器
    self.viewControllers = @[homeNavC, searchNavC, categoryNavC, settingNavC];
    
    //释放
    [homeVC release];
    [homeNavC release];
    [searchVC release];
    [searchNavC release];
    [categoryVC release];
    [categoryNavC release];
    [settingVC release];
    [settingNavC release];   
}

属性传值

HomeViewController.m
#import "HomeViewController.h"
#import "NextViewController.h"
#import "Singleton.h"
@interface HomeViewController ()

@property (nonatomic, retain) UITextField *textField;

@end

@implementation HomeViewController

- (void)dealloc {
    [_textField release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.948 green:1.000 blue:0.408 alpha:1.000];
     self.navigationItem.title = @"主页";
    
    //添加一个Button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, 200, 200);
    button.center = self.view.center;
    [button setTitle:@"GO" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:50];
    [button addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 175, 40)];
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    self.textField.placeholder = @"请输入内容";
    [self.view addSubview:_textField];
    [_textField release];
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"取值" style:UIBarButtonItemStylePlain target:self action:@selector(getValue)] autorelease];
}

- (void)getValue {
    Singleton *singleton = [Singleton sharedSingleton];
单例传值 self.textField.text
= singleton.string; } - (void)next { NextViewController *nextVC = [[NextViewController alloc] init];
属性传值 nextVC.
string = self.textField.text; [self.navigationController pushViewController:nextVC animated:YES]; [nextVC release]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
NextViewController.h
#import <UIKit/UIKit.h>

@interface NextViewController : UIViewController

@property (nonatomic, retain) NSString *string;

@end
NextViewController.m
#import "NextViewController.h"
@interface NextViewController ()
@end
@implementation NextViewController

- (void)dealloc {
    [_string release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.682 blue:0.933 alpha:1.000];
属性传值 self.navigationItem.title
= self.string; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end

                                        

 代理传值

SearchViewController .m
#import "SearchViewController.h"
#import "DetailViewController.h"

@interface SearchViewController ()<DetailViewControllerDelegate>

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"搜索";
    self.view.backgroundColor = [UIColor colorWithRed:0.757 green:1.000 blue:0.522 alpha:1.000];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemPlay) target:self action:@selector(play)];
}

- (void)play {
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    detailVC.delegate = self;
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release]; 
}

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

#pragma mark - DetailViewControllerDelegate
- (void)passString:(NSString *)string {
    self.navigationItem.title = string;
}
@end
DetailViewController.h
#import <UIKit/UIKit.h>

@protocol DetailViewControllerDelegate <NSObject>

- (void)passString:(NSString *)string;

@end

@interface DetailViewController : UIViewController

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

@end
DetailViewController.m
#import "DetailViewController.h"

@interface DetailViewController ()

@property (nonatomic, retain) UITextField *textField;

@end

@implementation DetailViewController

- (void)dealloc {
    [_textField release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
     self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.745 blue:0.791 alpha:1.000];
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 175, 40)];
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    self.textField.placeholder = @"请输入内容";
    [self.view addSubview:_textField];
    [_textField release];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if ([_delegate respondsToSelector:@selector(passString:)]) {
        [_delegate passString:_textField.text];
    }
}

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

       

 单例传值

Singleton.h
#import <Foundation/Foundation.h>

@interface Singleton : NSObject

@property (nonatomic, retain) NSString *string;

+ (Singleton *)sharedSingleton;

@end
Singleton.m
#import "Singleton.h"

@implementation Singleton

- (void)dealloc
{
    [_string release];
    [super dealloc];
}

+ (Singleton *)sharedSingleton {
    static Singleton *singleton = nil;
    if (singleton == nil) {
        singleton = [[Singleton alloc] init];
    }
    return singleton;
}
@end
CategoryViewController .m
#import "CategoryViewController.h"
#import "Singleton.h"
@interface CategoryViewController ()<UITextFieldDelegate>

@property (nonatomic, retain) UITextField *textField;

@end
@implementation CategoryViewController

- (void)dealloc
{
    [_textField release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor = [UIColor colorWithRed:0.720 green:1.000 blue:0.837 alpha:1.000];
     self.navigationItem.title = @"分类";
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 175, 40)];
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    self.textField.placeholder = @"请输入内容";
    self.textField.delegate = self;
    [self.view addSubview:_textField];
    [_textField release];

}

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

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    Singleton *singleton = [Singleton sharedSingleton];
    singleton.string = textField.text;
    [textField resignFirstResponder];
    return YES;
}
@end

        

Block传值

Block传值: 用于从后一个页面向前一个页面传值

后一个页面

1.Block类型重命名

2.Block属性

3.在合适的地方调用block, 并且传值

前一个页面

1.block赋值, 并使用传过来的值

Block类型

参数的个数由传值的个数决定, 参数的类型由传值的类型决定

void (^)(NSString *)

ViewController.h

重命名类型, 方便使用
typedef void (^BlockType)(NSString *string);

@interface ViewController : UIViewController

默认block是创建在栈区的, 使用copy可以把栈区block拷贝到堆区
@property (nonatomic, copy) BlockType block;

自动书写block, 为block赋值
- (void)passValue:(BlockType)block;
ViewController.m
#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, retain) UITextField *textField;

@end

@implementation ViewController

- (void)dealloc
{
    [_textField release];
    [_block release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.985 green:0.505 blue:1.000 alpha:1.000];
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 175, 40)];
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    self.textField.placeholder = @"请输入内容";
    [self.view addSubview:_textField];
    [_textField release];
    
    void(^helloWorld)() = ^() {
        NSLog(@"Hello World!");
    };
    helloWorld();
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.block(self.textField.text);
}

- (void)passValue:(BlockType)block {
    self.block = block;
}

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

@end
SettingViewController.m
#import "SettingViewController.h"
#import "ViewController.h"

@interface SettingViewController ()

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.701 green:0.716 blue:1.000 alpha:1.000];
     self.navigationItem.title = @"设置";
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [self.view addGestureRecognizer:tap];
    [tap release];
}

- (void)tap {
    ViewController *viewVC = [[ViewController alloc] init];
//    viewVC.block = ^(NSString *string){
//        self.navigationItem.title = string;
//    };
    [viewVC passValue:^(NSString *string) {
        self.navigationItem.title = string;
    }];
    [self.navigationController pushViewController:viewVC animated:YES];
    [viewVC release];
}

           

 

 

 

The one who wants to wear a crown must bear the weight!
原文地址:https://www.cnblogs.com/OrangesChen/p/4937316.html