Iphone 在TabBar应用程序中从一个视图更改为另一个视图时无法显示警报?
我正在应用程序中使用选项卡栏控制器 我可以通过单击选项卡上的按钮来更改视图,但当我单击选项卡按钮时,它会显示一个警报,供最终用户确认,如:“您应该离开此页面” 如果“是”,则导航,否则应保持在同一页面上 不过,警报不会显示。试试这个Iphone 在TabBar应用程序中从一个视图更改为另一个视图时无法显示警报?,iphone,objective-c,Iphone,Objective C,我正在应用程序中使用选项卡栏控制器 我可以通过单击选项卡上的按钮来更改视图,但当我单击选项卡按钮时,它会显示一个警报,供最终用户确认,如:“您应该离开此页面” 如果“是”,则导航,否则应保持在同一页面上 不过,警报不会显示。试试这个 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"myMSg" delegate:self cancelButtonTitle:@"Ok" otherButt
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"myMSg" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"cancel"];
[alert show];
[alert release];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
//Code for OK button i.e go to the next tab bar
}
if (buttonIndex == 1)
{
//Code for cancel button i.e dont load the next tab bar.
}
}
试试这个
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"myMSg" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"cancel"];
[alert show];
[alert release];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
//Code for OK button i.e go to the next tab bar
}
if (buttonIndex == 1)
{
//Code for cancel button i.e dont load the next tab bar.
}
}
这是alertview的代码,在单击tab按钮后调用的方法中调用它…并添加UIAlertView委托
UIAlertView *myAlert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"You should leave this page" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil,nil];
[myAlert show];
[myAlert release];
也重写该方法
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if(actionSheet==myAlert){
if (buttonIndex == 0)
{
//here call the corresponding page
}
}
else
{
//Do nothing
NSLog(@"cancel");
}
}
这是alertview的代码,在单击tab按钮后调用的方法中调用它…并添加UIAlertView委托
UIAlertView *myAlert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"You should leave this page" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil,nil];
[myAlert show];
[myAlert release];
也重写该方法
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if(actionSheet==myAlert){
if (buttonIndex == 0)
{
//here call the corresponding page
}
}
else
{
//Do nothing
NSLog(@"cancel");
}
}
您可以通过编程方式创建选项卡栏控制器,而不是在xib文件中创建它们,如下所示
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// set up a local nav controller which we will reuse for each view controller
UINavigationController *localNavigationController;
// create tab bar controller and array to hold the view controllers
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
// setup the first view controller (Root view controller)
RootViewController *myViewController;
myViewController = [[RootViewController alloc] initWithTabBar];
// create the nav controller and add the root view controller as its first view
localNavigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
// add the new nav controller (with the root view controller inside it)
// to the array of controllers
[localControllersArray addObject:localNavigationController];
// release since we are done with this for now
[localNavigationController release];
[myViewController release];
// setup the first view controller just like the first
ResortsListViewController *resortsListViewController;
resortsListViewController = [[ResortsListViewController alloc] initWithNibName:@"ResortsListView" bundle:nil];
resortsListViewController.title = @"Category1";
resortsListViewController.tabBarItem.image = [UIImage imageNamed:@"image1.png"];
resortsListViewController.navigationItem.title=@"a1";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:resortsListViewController];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
// setup the second view controller just like the first
RootViewController *rootViewController;
rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
rootViewController.title = @"a2";
rootViewController.tabBarItem.image = [UIImage imageNamed:@"image2.png"];
resortsListViewController.navigationItem.title=@"a2";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
// setup the third view controller just like the first
RootViewController *rootViewController;
rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
rootViewController.title = @"a3";
rootViewController.tabBarItem.image = [UIImage imageNamed:@"image3.png"];
rootViewController.navigationItem.title=@"ay3";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[rootViewController release];
// load up our tab bar controller with the view controllers
tabBarController.viewControllers = localControllersArray;
// release the array because the tab bar controller now has it
[localControllersArray release];
// add the tabBarController as a subview in the window
[window addSubview:tabBarController.view];
// need this last line to display the window (and tab bar controller)
[window makeKeyAndVisible];
}
&之后,为每个按钮使用警报视图:
UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Do you want to leave this page1" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil,nil];
[Alert show];
[Alert release];
[Alert setTag:1];
UIAlertView*Alert=[[UIAlertView alloc]initWithTitle:@“Alert”消息:@“您想离开此页面吗2”委托:自取消按钮:@“确定”其他按钮:无,无];
[警报显示];
[警报发布];
[警报设置标签:2]
&检查按下了哪个按钮:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ([alertView tag] == 1)
if (buttonIndex == 0)
{
//Code for OK button i.e go to the next tab bar
}
if (buttonIndex == 1)
{
//Code for cancel button i.e dont load the next tab bar.
}
}
您可以通过编程方式创建选项卡栏控制器,而不是在xib文件中创建它们,如下所示
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// set up a local nav controller which we will reuse for each view controller
UINavigationController *localNavigationController;
// create tab bar controller and array to hold the view controllers
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
// setup the first view controller (Root view controller)
RootViewController *myViewController;
myViewController = [[RootViewController alloc] initWithTabBar];
// create the nav controller and add the root view controller as its first view
localNavigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
// add the new nav controller (with the root view controller inside it)
// to the array of controllers
[localControllersArray addObject:localNavigationController];
// release since we are done with this for now
[localNavigationController release];
[myViewController release];
// setup the first view controller just like the first
ResortsListViewController *resortsListViewController;
resortsListViewController = [[ResortsListViewController alloc] initWithNibName:@"ResortsListView" bundle:nil];
resortsListViewController.title = @"Category1";
resortsListViewController.tabBarItem.image = [UIImage imageNamed:@"image1.png"];
resortsListViewController.navigationItem.title=@"a1";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:resortsListViewController];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
// setup the second view controller just like the first
RootViewController *rootViewController;
rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
rootViewController.title = @"a2";
rootViewController.tabBarItem.image = [UIImage imageNamed:@"image2.png"];
resortsListViewController.navigationItem.title=@"a2";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
// setup the third view controller just like the first
RootViewController *rootViewController;
rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
rootViewController.title = @"a3";
rootViewController.tabBarItem.image = [UIImage imageNamed:@"image3.png"];
rootViewController.navigationItem.title=@"ay3";
localNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[rootViewController release];
// load up our tab bar controller with the view controllers
tabBarController.viewControllers = localControllersArray;
// release the array because the tab bar controller now has it
[localControllersArray release];
// add the tabBarController as a subview in the window
[window addSubview:tabBarController.view];
// need this last line to display the window (and tab bar controller)
[window makeKeyAndVisible];
}
&之后,为每个按钮使用警报视图:
UIAlertView *Alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Do you want to leave this page1" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil,nil];
[Alert show];
[Alert release];
[Alert setTag:1];
UIAlertView*Alert=[[UIAlertView alloc]initWithTitle:@“Alert”消息:@“您想离开此页面吗2”委托:自取消按钮:@“确定”其他按钮:无,无];
[警报显示];
[警报发布];
[警报设置标签:2]
&检查按下了哪个按钮:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if ([alertView tag] == 1)
if (buttonIndex == 0)
{
//Code for OK button i.e go to the next tab bar
}
if (buttonIndex == 1)
{
//Code for cancel button i.e dont load the next tab bar.
}
}
我只想在某些特定视图上使用此警报。在我的应用程序中有5个选项卡,在所有选项卡中,我添加了1个导航控制器…我不需要每次在某些重要视图上单击tabbaritem时都发出警报…谢谢!!然后在该函数中,只需检查调用了哪个视图,并相应地显示警报……获取数组中的所有控制器,如以下数组=[[self-tabBarController]ViewController];然后检查要推送的控制器[[array objectAtIndex:0]sKindOfClass:[YourClassName class]]如果满足,则显示警报。我只希望在某些特定视图上显示此警报。在我的应用程序中,有5个选项卡,在所有选项卡中,我添加了1个导航控制器…我不需要每次在某些重要视图上单击tabbaritem时都发出警报…谢谢!!然后在该函数中,只需检查调用了哪个视图,并相应地显示警报……获取数组中的所有控制器,如以下数组=[[self-tabBarController]ViewController];然后检查要推送的控制器[[array objectAtIndex:0]sKindOfClass:[YourClassName class]]是否满足,然后显示警报。。