1

我有一个菜单

在此处输入图像描述

它只显示选择按钮,但当我选择 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

4 回答 4

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);
}
于 2013-05-29T09:21:42.153 回答
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;
}
于 2013-05-29T09:34:33.520 回答
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];
}
于 2013-05-29T09:38:47.543 回答
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

于 2013-05-29T09:45:56.027 回答