如何通过菜单按钮打开新的视图控制器?

时间:2013-05-29 09:08:04

标签: ios objective-c xcode uibutton

我有一个像

这样的菜单

enter image description here

它只显示选择按钮,但我想选择Hello 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;
 }

4 个答案:

答案 0 :(得分:4)

在选择表

中调用委托方法
- (void) niDropDownDelegateMethod: (NIDropDown *) sender 

找出从那里按下的选项并实现推送方法

问题是代码没有提供获取所选值的方法。您必须查看该自定义类NIDropDown并添加一个返回所选索引的值

解决方案

.h add

@property (nonatomic, assign) NSInteger *selectedIndex;

的.m

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

并在viewcontroller中

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

答案 1 :(得分: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时,您可以使用button标签在

中打开一个新的视图控制器
-(void)rel{
   // use button tag to open respective view controller here.
  dropDown = nil;
}

答案 2 :(得分: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];
}

答案 3 :(得分: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];
    }
}

likeWise你可以检查索引,并根据你可以显示ViewController

相关问题