頁面(ViewController)之間的跳轉與資料傳遞,是每個 iOS 應用都會遇到的基礎課題。以下整理幾種常見的跳轉方式與資料回傳策略。
// 進入子頁面 [self.navigationController pushViewController:subTableViewController animated:YES]; // 返回上一頁 [self.navigationController popViewControllerAnimated:YES];
UIViewController *control = [[UIViewController alloc] init]; [self presentModalViewController:control animated:YES]; [control release]; // 關閉模態 [self dismissModalViewControllerAnimated:YES];
[self.view.window addSubview:otherview]; [self.view removeFromSuperview];
// 註冊監聽
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(setData:)
name:kNotificationMessage
object:nil];
// 發送訊息
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationMessage
object:self
userInfo:infoDict];
選擇哪一種,取決於資料量、生命週期與團隊的架構慣例。一般來說,delegate 適合回傳結果、通知適合跨多層廣播、DB 與 NSUserDefaults 則負責持久化。