码迷,mamicode.com
首页 > 移动开发 > 详细

iOS中的视图跳转的三种方式(代码跳转,根据桥跳转,按钮跳转)

时间:2015-09-13 22:50:14      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:

#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@property (retain, nonatomic) IBOutlet UITextField *textField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)pushButtonAction:(id)sender {
    
    //跳转第一种方式
//    SecondViewController *secondVC = [[SecondViewController alloc] init];
//    [self.navigationController pushViewController:secondVC animated:YES];
    //1.当要获取的视图控制器对象和当前视图控制器在同一个storyboard文件中
    //从 storyboard 获取对应的视图控制器对象
    //self.storyboard 获取当前视图控制器所在的 storyboard对象
    /*
   SecondViewController *secondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"second"];
    [self.navigationController pushViewController:secondVC animated:YES];
      //2.当要获取的视图控制器对象和当前视图控制器在同一个storyboard文件中
    //1.先创建storyboard对象
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Leo" bundle:nil];
     
     
     
    //假设,另外一个storyboard的名字是Leo.stroyboard
    //(2)从对应的storyboard中根据标识符,获取视图控制器对象
    SecondViewController *second = [storyboard instantiateViewControllerWithIdentifier:@"reuse"];
    //然后再push 即可
     */
    //跳转第二种,根据桥跳转
    //[self performSegueWithIdentifier:@"reuse" sender:nil];
    
    
    //跳转第三种方式
    //直接将按钮与下一个界面建桥即可,不用写任何代码,但是该方法只有一个弊端,因为这种方法为单个按钮单独定制的跳转,如果一个界面中有多个按钮都要跳转到这一个界面,还需要增加多个桥,这样的话,关系非常的冗杂,不如直接通过第二种方式(视图控制器与视图控制器之间建桥,让多个button点击方法实现跳转即可)
    
}
//通过桥(segue)完成页面的跳转,在跳转之前该方法就会出触发,一般用于传值.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    //获取下个界面的对象
   SecondViewController *secondVC = segue.destinationViewController;
    //传值
    secondVC.data = self.textField.text;
    
}

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

- (void)dealloc {
    [_textField release];
    [super dealloc];
}
@end
SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController
@property (nonatomic, copy) NSString *data;
@end


SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()
@property (retain, nonatomic) IBOutlet UILabel *aLable;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.aLable.text = self.data;
}

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

- (void)dealloc {
    [_aLable release];
    self.data = nil;
    [super dealloc];
}
@end

 

iOS中的视图跳转的三种方式(代码跳转,根据桥跳转,按钮跳转)

标签:

原文地址:http://www.cnblogs.com/wohaoxue/p/4805636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!