ios 学习笔记,tabbarcontroller 实现底部导航

要求:通过tabbarcontroller实现一个底部导航,并且实现两个界面之间通过button切换,互相传值

参考博客

tabbar部分

AppDelegate.m

#import "AppDelegate.h"
#import "ViewControl1.h"
#import "ViewController2.h"
#import "ViewController3.h"
#import "ViewController4.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    //创建windows
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.window.backgroundColor = [UIColor whiteColor];

    //初始化一个tabbarcontroller
    UITabBarController *  bottomTabbarController = [[UITabBarController alloc ]init];
    self.window.rootViewController = bottomTabbarController ;

    //创建4个子控制器
    ViewControl1 * view1 = [[ViewControl1 alloc]init];
    view1.tabBarItem.title = @"view1";

     ViewController2* view2 = [[ViewController2 alloc]init];
    view2.tabBarItem.title = @"view2";

    ViewController3 * view3 = [[ViewController3 alloc]init];
    view3.tabBarItem.title = @"view3";

    ViewController4 * view4 = [[ViewController4 alloc]init];
    view4.tabBarItem.title = @"view4";

    //添加子类控制器

    bottomTabbarController.viewControllers = @[view1,view2,view3,view4];

    //显示windows为主窗口

    [self.window makeKeyAndVisible] ;



    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {

}

- (void)applicationDidEnterBackground:(UIApplication *)application {

}

- (void)applicationWillEnterForeground:(UIApplication *)application {

}

- (void)applicationDidBecomeActive:(UIApplication *)application {
}

- (void)applicationWillTerminate:(UIApplication *)application {
}

@end

1.在viewcontroller中用self.tabbarcontroller 取得tabbarcontrolle。
2. 给item添加image,view.tabbarItem.imgge.点击是的图片 view.tabbarItem.selectimgge

每一个viewcontroller

#import "ViewController3.h"

@interface ViewController3 ()

@end

@implementation ViewController3

- (void)viewDidLoad {
    [super viewDidLoad];

     _labelOFView3 = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 30)];
    _labelOFView3.text = @"this is view3";
    [self.view addSubview:_labelOFView3];
}

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

实现界面切换和传值

正向传值用的是属性传值,反向传值用的是block传值

viewcontroller1

#import 
#import "ViewController2.h"

@interface ViewControl1 : UIViewController
@property(nonatomic ,strong )UILabel* labelOFView1;
@end


#import "ViewController2.h"
#import "ViewControl1.h"

@interface ViewControl1 ()

@property(nonatomic ,strong)UITextField * toScondTextField;
@property(nonatomic,strong )UIButton * sendButton  ;

@end

@implementation ViewControl1

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _labelOFView1 = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 30)];
    _labelOFView1.text = @"this is view1";
    [self.view addSubview:_labelOFView1];

    self.toScondTextField;
    [self.sendButton addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside];

}

-(void)onclick:(id)sender{
    NSLog(@"click view1");


    ViewController2 * vc2 = [self.tabBarController.viewControllers objectAtIndex:1];
    vc2.str = _toScondTextField.text;
    [vc2 returnText:^(NSString *text) {
        self.labelOFView1.text =text;
    }];
//    [self presentViewController:vc2 animated:YES completion:nil];
    [self.tabBarController setSelectedIndex:1];

}

-(UIButton *) sendButton{
    if (!_sendButton) {
        _sendButton = [[UIButton alloc]initWithFrame:CGRectMake(50, 150, 200,30)];
        [_sendButton setTitle:@"to second View" forState:UIControlStateNormal];
        [_sendButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _sendButton.backgroundColor = [UIColor grayColor];
        [self.view addSubview:_sendButton];
    }
    return _sendButton  ;
}

-(UITextField *)toScondTextField{
    if (!_toScondTextField) {
        _toScondTextField = [[UITextField alloc] initWithFrame:CGRectMake(50, 200, 100, 30) ];
        _toScondTextField.placeholder = @"input here";
        [self.view addSubview:_toScondTextField];
    }
    return  _toScondTextField   ;
}



-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

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


@end

viewcontroller2

#import 

typedef void (^returnStringBlock)(NSString * text);

@interface ViewController2 : UIViewController

@property(nonatomic ,strong )UILabel* labelOFView2;
@property(nonatomic,strong) UITextField * toFirstViewTextView;
@property(nonatomic,strong)UIButton * okButton;

@property(nonatomic,copy) returnStringBlock textBlock;
@property(nonatomic,copy)NSString* str;

-(void) returnText : (returnStringBlock) block;

@end




#import "ViewController2.h"

@interface ViewController2 ()


@end

@implementation ViewController2


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _labelOFView2 = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 200, 30)];
    if (_str) {
        _labelOFView2.text = [NSString stringWithFormat:@"this is view2 %@",_str];
    }else{
        _labelOFView2.text = @"this is view2";

    }

    [self.view addSubview:_labelOFView2];
    self.toFirstViewTextView    ;

    [self.okButton addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)returnText:(returnStringBlock)block{
    self.textBlock = block  ;
}

-(void)onclick:(id)sender{
//    [self dismissViewControllerAnimated:YES completion:nil];
    if (self.textBlock) {
        self.textBlock(_toFirstViewTextView.text);
    }
    [self.tabBarController setSelectedIndex:0];
}
-(void)viewDidDisappear:(BOOL)animated{

}

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



-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

//懒加载
-(UIButton*) okButton{
    if (!_okButton) {
        _okButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 150, 60, 35)];
        [_okButton setTitle:@"ok" forState:UIControlStateNormal];
        [_okButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _okButton.backgroundColor = [UIColor grayColor];
        [self.view addSubview:_okButton];
    }
    return _okButton;
}

-(UITextField*)toFirstViewTextView{
    if (!_toFirstViewTextView) {
        _toFirstViewTextView = [[UITextField alloc]initWithFrame:CGRectMake(50, 200, 200, 30)];
        _toFirstViewTextView.placeholder = @ "send to firsr view text";
        [self.view addSubview:_toFirstViewTextView];
    }
    return _toFirstViewTextView;
}

@end

1.注意在控制器中取得切换 item的方法
[self.tabbarcontroller setselectIndex:];
2.在控制器中取得子视图
[self.tabbarcontroller.viewcontrollers objectIndex:];

你可能感兴趣的:(ios)