2012-03-09 55 views
0

我想使我的UIActionSheet中的按鈕使用下面的代碼執行視圖更改。UIActionSheet按鈕來顯示視圖

-(void)displayActionSheet:(id)sender 
    { 

actionSheet = [[UIActionSheet alloc] 
           initWithTitle:nil 
           delegate:nil 
           cancelButtonTitle:@"Cancel" 
           destructiveButtonTitle:nil 
           otherButtonTitles:@"Devanagari", @"English", nil]; 

actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque; 

[actionSheet showInView:self.view ]; 

[actionSheet release]; 

} 

如何修改此代碼以使梵文和英語按鈕鏈接到各自的視圖?

回答

1

爲了減少混淆,您可能需要考慮命名actionSheet而不是actionSheet。

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if(actionSheet == actionSheet) 
     { 

switch (buttonIndex) 
      { 
       case 0: 
       { 
      DevanagariViewController *controller1 = [[DevanagariViewController alloc] initWithNibName:@"DevanagariViewController" bundle:nil]; 
[self presentModalViewController:controller1 animated:NO]; 
       NSLog(@"DevanagariViewController"); 
       break; 
       } 


       case 1: 
       { 
          EnglishViewController *controller2 = [[EnglishViewController alloc] initWithNibName:@"EnglishViewController" bundle:nil]; 
[self presentModalViewController:controller2 animated:NO]; 
       NSLog(@"EnglishViewController"); 
       break; 
       } 


      } 


     } 
+2

不僅減少混亂,'actionSheet == actionSheet'始終計算爲'真'。你可能應該改變參數名稱,或者使用'self-> actionSheet'或者使其成爲一個屬性並使用'self.actionSheet' – Sulthan 2012-03-09 23:54:33

2
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex]; 
    if([title isEqualToString:@"Devanagari"]) { 
     //Code if Devanagari button is pressed 
    } 
    if([title isEqualToString:@"English"]) { 
     //Code if English button is pressed 
    } 
}