2011-11-18 64 views

回答

2

這是alertview代碼,請撥打本在它獲取標籤按鈕被點擊後調用的方法....並添加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"); 
     } 

     } 
+0

我只希望當我在一些具體view..In我的應用程序此警報有5個選項卡,並在所有的TabBar我有加1個導航控制器。 ..我不需要警惕每次當我點擊tabbaritem只是當我在一些重要的視圖...謝謝! – Developer

+1

然後在該函數中檢查哪個視圖正在調用,並相應地顯示警報.....獲取像這樣的數組中的所有控制器= [[self tabBarController] viewControllers];然後檢查控制器是否被推送[[array objectAtIndex:0] sKindOfClass:[YourClassName class]],如果滿足則顯示警報。 – Minakshi

1

試試這個

 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. 
     } 
    } 
0

您可以以編程方式創建選項卡欄控制器而不是創建廷它們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"]; 
[email protected]"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"]; 
[email protected]"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"]; 
[email protected]"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中*警報= [[UIAlertView中的alloc] initWithTitle:@「警報「message:@」你想離開這個頁面2「delegate:self cancelButtonTitle:@」ok「otherButtonTitles:nil,nil]; [警示]; [警報發佈]; [Alert setTag: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. 
     } 
    } 
相關問題