使用者點擊「我的收藏」按鈕 → 未登入 → 彈出登入頁 → 登入成功 → 自動導向「我的收藏」頁。
// 登入成功的回調中
- (void)loginSuccess {
// 1. 取得導航控制器
UINavigationController *nav = self.navigationController;
// 2. 初始化目標頁面
MyFavoritesViewController *favVC = [[MyFavoritesViewController alloc] init];
// 3. 推入堆疊(自帶動畫與返回鍵)
[nav pushViewController:favVC animated:YES];
}
// 登入頁面屬性
@property (nonatomic, strong) Class targetVCClass;
@property (nonatomic, copy) NSString *targetStoryboardID;
// 登入成功
- (void)loginSuccess {
UIViewController *targetVC;
if (self.targetVCClass) {
targetVC = [[self.targetVCClass alloc] init];
} else if (self.targetStoryboardID) {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
targetVC = [sb instantiateViewControllerWithIdentifier:self.targetStoryboardID];
}
[self.navigationController pushViewController:targetVC animated:YES];
}
// 呼叫端
LoginViewController *loginVC = ...;
loginVC.targetVCClass = [MyFavoritesViewController class];
// 或
loginVC.targetStoryboardID = @"MyFavoritesVC";
// 無 UINavigationController 時
[self presentViewController:favVC animated:YES completion:nil];
// 或切換 window.rootViewController(適用於登入後整個 App 狀態變更)
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
app.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:favVC];
登入成功後儲存 Token,後續啟動時在 AppDelegate 判斷自動進入主頁。
pushViewController 保持導航堆疊一致性。核心一句:**登入成功 → 取得 navigationController → push 目標 VC**,簡單可靠。