1. 程式人生 > >ios NavigationViewController跳轉以及返回傳值

ios NavigationViewController跳轉以及返回傳值

spl art com osi nts isp lai -i rgb


(一)使用NavigationViewController進行頁面跳轉時,應該使用pushViewController方法來跳轉至下一頁面。這種話。下一頁面相同在NavigationViewController容器中。

1、跳轉到下一頁面:

PowerViewController *power = [[PowerViewController alloc] init];

//所要跳轉頁面PowerViewController中有個屬性dictionary1是個NSMutableDictionary類型的容器 [power.dictionary1 setObject:[self.outTxtPass text] forKey:ALONE_SITEPRIZE_PWD];
//使用pushViewController跳轉到下一頁面 [self.navigationController pushViewController:power animated:YES];

?

2、從當前頁面返回到上一頁面並傳值過去:

//此頁面已經存在於self.navigationController.viewControllers中,而且是當前頁面的前一頁面

PowerViewController *power = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-
2]; //初始化其屬性 power.dictionary = nil; //傳遞參數過去 power.dictionary = [NSMutableDictionary dictionaryWithDictionary:self.dictionary1]; //使用popToViewController返回並傳值到上一頁面 [self.navigationController popToViewController:power animated:YES]; 返回到上一頁後,上一頁面顯示後要接收參數,並刷新。

註意此時應該在viewDidAppear中進行推斷並接收傳遞的值:

-(void)viewDidAppear:(BOOL)animated { //推斷並接收返回的參數 }
3、從當前頁面返回到上一頁面(無需傳值)的方法:
//返回到上一界面 -(IBAction)backOff:(id)sender { [self.navigationController popViewControllerAnimated:true]; }


(二)關於ios中 viewcontroller的跳轉問題。當中有一種方式是採用navigationController pushViewController 的方法,比方我從主頁面跳轉到了一級頁面,又從一級頁面跳轉到了二級頁面。然後從二級頁面跳轉到了三級頁面,依次類推。假設一級一級的返回我知道是沒有問題的。調用navigationController popViewControllerAnimated即可了。

可是某些情況下我可能想要立即回到主頁面,而不是一級一級的返回(假設有非常多層會非常累的),那該怎麽辦呢?

1.返回根頁面vc用

[self.navigationController popToRootViewController]

2.返回根頁面vc用 :

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:2] animated:YES];

或(通過class定位)

for (UIViewController *controller in self.navigationController.viewControllers) { if ([controller isKindOfClass:[TargetController class]]) { [self.navigationController popToViewController:controller animated:YES]; } }



ios NavigationViewController跳轉以及返回傳值