天天看點

IOS StoryBoard視圖切換傳值

IOS StoryBoard頁面傳值,在沒有StoryBoard之前,xib直接傳值我們可以通過協定,下面我們主要說一下StoryBoard界面切換的傳值

StoryBoard傳值方法一(使用segue傳值)

首先新建立一個Project,建立完成以後,打開StoryBoard,在目前的viewcontroller中分别拖放一個UIButton,一個UILabel,一個UITextField,然後向StoryBoard中拖放一個viewcontroller,在新拖放的viewcontroller上面拖放兩個UITextFiled,一個UILabel,一個UIButton,拖放完成以後如下圖:

IOS StoryBoard視圖切換傳值

在第一個UIViewController的類中定義一個UITextField,指向界面上的UITextField,然後建立一個類SecondViewController,指向第二個viewcontroller,由于第二個viewcontroller界面上有兩個UITextField,我們在SecondViewController的頭檔案中聲明兩個UITextField,分别指向界面上的UITextField,假設我們要從第一個viewcontroller向界面傳遞三個參數,分别是字元串name,字元串price,鍵值對dic,那麼我們在SecondViewController.h中聲明,同時在.m檔案中實作,SecondViewController的代碼如下:

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property(nonatomic,strong)IBOutlet UITextField *nameField;

@property(nonatomic,strong)IBOutlet UITextField *priceField;

@property(nonatomic,strong)NSString *name;

@property(nonatomic,strong)NSString *price;

@property(nonatomic,strong)NSDictionary *dic;


-(IBAction)BtnGoToNext:(id)sender;

@end
           
#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize name;
@synthesize price;
@synthesize dic;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.nameField.text = name;
    self.priceField.text =price;
    NSLog(@"dic:%@",dic);
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}
//使用通知傳遞參數,發送廣播
-(void)BtnGoToNext:(id)sender{
    
    //three 是連接配接線的identifier,如果是tablecell的click時間,這兒可以傳目前點選的cell
    [self performSegueWithIdentifier:@"three" sender:self];
    NSArray *array =[NSArray arrayWithObjects:@"one",@"two",@"three", nil];
    NSNotification *notification =[NSNotification notificationWithName:@"notification1" object:array];
    [[NSNotificationCenter defaultCenter] postNotification:notification];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"notification2" object:self.priceField.text];
}

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

@end</span>
           

打開第一個viewcontroller對應的類檔案,代碼如下:

<span style="font-family:Comic Sans MS;font-size:14px;">#import <UIKit/UIKit.h>

@interface LTViewController : UIViewController

@property(nonatomic,strong)IBOutlet UITextField *txtField;

@end


#import "LTViewController.h"

@interface LTViewController ()

@end

@implementation LTViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

//怎樣跳轉到随意一個頁面

//在有可能進行上級跳轉的ViewController檔案裡加上以下代碼,函數名稱任起:

#pragma mark 定義這個函數,别的ViewController在Exit的時候就能直接跳到這了
//在想要跳轉view的Exit上右鍵,選擇這個goHome函數,拉到想要運作的button上,就能夠實作跳轉了(隻需要有一個UIStoryboardSegue的參數)
- (IBAction)goHome:(UIStoryboardSegue *)segue
{
    //segue.identifier
    [[segue sourceViewController] class];
}




//使用segue傳遞參數
//注意:發送資料時,[send setValue:msg forKey:@"name"]; 這個"name"名稱一定要與跳轉後界面的聲明的類型對象的命名一緻,不然的話,跳轉後的界面是收不到傳遞的值的。
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSString *content = self.txtField.text;
    UIViewController *next = segue.destinationViewController;
    
    //UIButton *btn =(UIButton *)sender;//跳轉觸發的對象
    //segue.identifier
    
    if([next respondsToSelector:@selector(setName:)]){//setName:是下一個viewcontroller的中定義的屬性自動生成的方法名
        [next setValue:content forKey:@"name"];
        [next setValue:@"10.3" forKey:@"price"];
        
        NSDictionary *dictionary =[NSDictionary dictionaryWithObjectsAndKeys:@"one",@"key",@"three",@"sex",nil];
        [next setValue:dictionary forKey:@"dic"];
    }
}


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

@end
           

我們在第一個類型重寫prepareForSegue方法,在第二個類的didViewLoad方法中接收傳遞過來的參數

讓第一個viewcontroller上面的Button指向第二個viewcontroller,如下圖:

IOS StoryBoard視圖切換傳值

這樣就完成了參數的傳遞

StoryBoard傳值方法二(使用廣播)

向storyboard上面再次拖放一個viewcontroller,在上面拖放一個UILabel,一個UITextField,拖放完成以後如下圖:

IOS StoryBoard視圖切換傳值

然後建立一個類,繼承自UIViewController,命名為ThreeViewController,指向第三個viewcontroller,我們在.h檔案中聲明一個UITextField,指向頁面的UITextField,我們在該類的ViewDidLoad函數中聲明觀察者,代碼如下:

IOS StoryBoard視圖切換傳值
IOS StoryBoard視圖切換傳值

我們通過performSegueWithIdentifier完成界面的跳轉,我們在SecondViewController聲明Button的點選方法,如下圖:

IOS StoryBoard視圖切換傳值

讓第二個viewcontroller上Button為Go To Three的按鈕點選指向執行BtnGoToNext,讓第二個viewcontroller的manual指向第三個,設定連接配接線的identifier為three,BtnGoToNext的實作如下圖:

IOS StoryBoard視圖切換傳值

這樣就完成了參數的傳遞

代碼下載下傳