天天看點

iOS 之 跳轉到新頁面 2個頁面間跳轉和資料傳遞

今天,介紹一下2個多個頁面 界面 到跳轉。首先普及一下基礎知識:每個界面用storyboard來設計,然後每個界面有自己到viewcontroller。這是必須的。

一,界面跳轉

ios7的storyboard模式,所有的界面都在同一個 storyboard中。我們打開主story。新加一個view controller。拖動到右邊就行。

然後有2種方式添加 關聯:

第一種

在第一個view controller中加一個button。點中button,control+滑鼠左鍵(或者滑鼠右鍵拖動)拖動到第二個view controller中。選push方式,model,custum方式都行。

然後run 就行。看看效果吧。

第二種:代碼方式

選第一個view controller,右鍵拖動到第二個view controller中,選custom方式(其他方式都行)。然後在2個view之間 有一條線,這跳線 就是segue。選中這跳線,給他來個identifier,比如叫 secondView

然後給第二個view 關聯view controller 的class 

選中 view controller 再選 custom class 設定為 SecondViewController 這就是把 這個控件和一個類關聯起來了。

給第一個view controller 中添加一個button,添加一個 touch up inside 事件,函數如下,具體怎麼添加,看我以前到blog

- (IBAction)sender:(id)sender

{

    NSLog(@"%@",@"wwww");

    [selfperformSegueWithIdentifier:@"secondView"sender:self];

//    [self presentModalViewController:nil animated:YES];

}

然後run 就行。點button後 就打開第二頁了。

二,界面傳值

在第一個storyboard的第一個view controller。m檔案中,加入

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

   if([segue.identifierisEqualToString:@"secondView"])

    {

       id theSegue = segue.destinationViewController;

        [theSeguesetValue:elementarrayforKey:@"elementarray"];

    }

}

就行了。

[theSegue setValue:elementarray forKey:@"elementarray"]; 這句中的 elementarray 就是要傳的變量。

在第二個view controller控制的view controller。h中

//傳過來的result,變量

@property (nonatomic,strong)NSMutableArray *elementarray;

。m中

@synthesize elementarray;

-(void) viewDidLoad

{

   NSLog(@"%lu", (unsignedlong)elementarray.count);

    for (NSMutableDictionary *tin elementarray)

    {

       NSLog(@"%@", [tobjectForKey:@"name"]);

    }

}

就可以接收到了。就是注意第二個view到類型 要和第一個view中變量的類型完全一緻即可。

别的沒了。

附上segue的圖

iOS 之 跳轉到新頁面 2個頁面間跳轉和資料傳遞