2013-05-29 81 views
1

我有這樣如何通過菜單按鈕打開新的視圖控制器?

enter image description here

菜單它只顯示選擇按鈕,但我想,當我選擇你好2開Hello2視圖控制器。

我的代碼:

enter code here 

- (IBAction)selectClicked:(id)sender { 
NSArray * arr = [[NSArray alloc] init]; 
arr = [NSArray arrayWithObjects:@"Hello 0", @"Hello 1", @"Hello 2", @"Hello 3", @"Hello 4", @"Hello 5", @"Hello 6", @"Hello 7", @"Hello 8", @"Hello 9",nil]; 
NSArray * arrImage = [[NSArray alloc] init]; 
arrImage = [NSArray arrayWithObjects:[UIImage imageNamed:@"apple.png"], [UIImage imageNamed:@"apple2.png"], [UIImage imageNamed:@"apple.png"], [UIImage imageNamed:@"apple2.png"], [UIImage imageNamed:@"apple.png"], [UIImage imageNamed:@"apple2.png"], [UIImage imageNamed:@"apple.png"], [UIImage imageNamed:@"apple2.png"], [UIImage imageNamed:@"apple.png"], [UIImage imageNamed:@"apple2.png"], nil]; 
if(dropDown == nil) { 
    CGFloat f = 200; 
    dropDown = [[NIDropDown alloc]showDropDown:sender :&f :arr :arrImage :@"down"]; 
    dropDown.delegate = self; 
} 
else { 
    [dropDown hideDropDown:sender]; 
    [self rel]; 
} 
} 

-(void)rel{ 
// [dropDown release]; 
    dropDown = nil; 
} 
+0

設置一些標記按鈕和按按鈕選擇,檢查相應地使用標記和打開視圖控制器來單擊哪個按鈕。 – Girish

+0

什麼是NIDropDown類?是否存在表視圖的didSelectRowAtIndexPath方法? –

+0

@SharanyaKM在NIDropDown.m中有這種方法http://pastie.org/7978703 – Mhmt

回答

3

好吧..所以,如果我沒有錯這是你正在使用的課程https://github.com/BijeshNair/NIDropDown/blob/master/NIDropDown.m

如果你看到這個方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

// Before you do hiding of the table just pass the indexPath as a tag to the button. 
btnSender.tag = indexPath.row; 
[self hideDropDown:btnSender]; 
} 

所以,當你-rel叫您可以使用該按鈕標籤在

-(void)rel{ 
    // use button tag to open respective view controller here. 
    dropDown = nil; 
} 
4

委託方法表

- (void) niDropDownDelegateMethod: (NIDropDown *) sender 

的選擇被調用找出從那裏壓的選項並執行push方法

問題代碼沒有提供獲取選定值的方法。您必須查看該自定義類NIDropDown並添加一個返回選擇的值泰德指數

解決方案

.H添加

@property (nonatomic, assign) NSInteger *selectedIndex; 

.M

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    self.selectedIndex=indexPath.row;//Add this line 
    [self myDelegate]; 
} 

和視圖 - 控制

- (void) niDropDownDelegateMethod: (NIDropDown *) sender { 
    NSLog(@"%d",sender.selectedIndex); 
} 
2

打開一個新的視圖控制器通過這樣的代碼做一些修改你可以這樣做

NIDropDown.h添加此代碼

@property (nonatomic, retain) NSString * str; 

NIDropDown.m添加以下代碼

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [self hideDropDown:btnSender]; 

    UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath]; 
    [btnSender setTitle:c.textLabel.text forState:UIControlStateNormal]; 

    NSLog(@"%@",c.textLabel.text); 

    self.str = c.textLabel.text; //here we are setting our str variable for later use 

    for (UIView *subview in btnSender.subviews) { 
     if ([subview isKindOfClass:[UIImageView class]]) { 
      [subview removeFromSuperview]; 
     } 
    } 
    imgView.image = c.imageView.image; 
    imgView = [[UIImageView alloc] initWithImage:c.imageView.image]; 
    imgView.frame = CGRectMake(5, 5, 25, 25); 
    [btnSender addSubview:imgView]; 
    [self myDelegate]; 
} 

NIViewController.m添加以下代碼

- (void) niDropDownDelegateMethod: (NIDropDown *) sender { 
    if ([sender.str isEqualToString:@"Hello0"]){ 
     //Put hello0VC transition code 
     NSLog(@"dilip hello0"); 
    }else if ([sender.str isEqualToString:@"Hello1"]) { 
     //Put hello1VC transition code 
     NSLog(@"dilip hello1"); 
    }else if ([sender.str isEqualToString:@"Hello2"]){ 
     //Put hello2VC transition code 
     NSLog(@"dilip hello2"); 
    }else if ([sender.str isEqualToString:@"Hello3"]){ 
     //Put hello3VC transition code 
     NSLog(@"dilip hello3"); 
    }else if ([sender.str isEqualToString:@"Hello4"]){ 
     //Put hello4VC transition code 
     NSLog(@"dilip hello4"); 
    } 
    [self rel]; 
} 
2

對於您可以進行自定義PickerView並在這,只要你想,你可以得到這樣的看法。

而且你必須實現pickerView的委託方法:

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row 
     inComponent:(NSInteger)component 
{ 
    if (row==0) 
    { 
     Hello1VC *VC1=[[Hello1VC alloc]initWithNibName:@"Hello1VC" bundle:nil]; 
     [self presentModalViewController:VC1 animated:YES]; 
    } 
} 

同樣,你也可以檢查指標,並根據您可以顯示視圖控制器

相關問題