如果你的標籤欄控制器連接到「IBOutlet
」(或有其他的參考),切換標籤就像更新selectedIndex
屬性(我爲你鏈接了Apple文檔)一樣簡單。
編輯:
你的代碼改成這樣。
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// if you have connected your tab bar controller to an IBOutlet named myTabBarController
if(myTabBarController)
{
// first tab bar controller is zero, second tab bar controller is 1, etc.
myTabBarController.selectedIndex = 1;
} else {
NSLog(@"tabBarController is nil and probably not set correctly");
}
}
- (void)pressedButton:(id)sender {
UIAlertView * alertView =
[[UIAlertView alloc] initWithTitle:@"Snap it" message:@"Take a picture" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
if(alertView)
{
[alertView show];
}
}
此處假設您已將標籤欄控制器正確設置爲IBOutlet。另請注意,我已將警報視圖上的「委託」設置爲「自我」(即如果使用「self
」作爲委託,則「clickedButtonAtIndex
」方法必須與&類別相同)。
感謝您的反饋。我非常新的編碼,我怎麼知道它是否連接到「IBOutlet」? –
如果您的標籤欄控制器存在於您的故事板(或xib文件)中,您可以將其設置爲「IBOutlet」並以此方式訪問它。如果您完全使用代碼創建標籤欄,則可以將其引用爲實例變量。 –
我發現了IBOutlet: @property(nonatomic,retain)IBOutlet UITableView * stepsTableView; 現在如何鏈接它的消息框? 乾杯, F –