1. 程式人生 > >ios 學習筆記,tabbarcontroller 實現底部導航

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 <UIKit/UIKit.h>
#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<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

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


@end

viewcontroller2

#import <UIKit/UIKit.h>

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<UITouch *> *)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:];