2012-06-28 20 views
0

我刚刚加入本网站后,非常难以回答我的问题,并且你们在做什么时看起来很神奇。我想为iPhone构建一个简单的介绍应用程序。我遇到的问题是我想使用UIPickerView来选择各种选项,在pickerview中我有大约20个选项。接下来我想要做的是在单击选项时能够打开下一个窗口,我希望在pickerview中为每个选项设置单独的窗口。基本上我有一个品牌,那么当你在pickerview上选择品牌时选择品牌,你就会转向为该品牌制造的正确配件。每个品牌都有独立的配件,这就是为什么我需要每个品牌都有不同的新窗口。我已经有了我的pickerview,我只是不知道如何继续下一步。一旦我通过点击在选取器上选择一个项目,我希望它移动到相应的下一个窗口。预先感谢您的帮助。如何将UIPickerView选项连接到单独的页面

回答

2

我会假设你的声明时UIPickerView,你已经设置委托给当前视图无论是在界面生成器或代码即myPicker.delegate = self;这样做。同样在你的.h文件中,你已经设置了控制器,使它类似于myViewController: UIViewController<UIPickerViewDelegate>

您将实现以下方法和使用参数row决定哪些查看下显示。在选择一行之后,此方法被触发(并且在您指定哪个类是选择器视图的委托的时刻进行连线)。您使用的是navigationController假设,它可能看起来像:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 
    //For example, you can do a switch based on the row 
    switch (row) { 
     case 0: 
      [self.navigationController pushViewController:myFirstView animated:YES]; 
      break; 
     case 1: 
      [self.navigationController pushViewController:mySecondView animated:YES]; 
      break; 
     // .. 
     // .. 
     case 19: 
      break; 
     default: 
     //... 
    } 
} 

请注意,如果您声明一个新的变量(如您的视图控制器之一)的情况下语句中,你需要的情况下,在大括号{ }声明为了这样做。

或者,如果您不希望事件在选中某行后自动触发(例如,如果用户意外地选择了一行),您可以创建一个按钮以供用户按下以确认选择/继续。按下按钮后,按钮的目标方法可以根据选择器中选定的行进行操作。您可以通过以下方法调用[myPicker selectedRowInComponent:0](假定您的选择器只有一列/“组件”)来查找选定的行。

祝你好运!

编辑(读你的后续问题后):

假设SingleComponentPickerViewController.h就是您创建UIPickerView的看法。

SingleComponentPickerViewController.h:

@interface SingleComponentPickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> 

SingleComponentPickerViewController.m:

在您的viewDidLoad,(或你创建的pickerview),设置委托:

-(void) viewDidLoad { 
    //... 
    myPickerView.delegate = self; 
    //... 
    [super viewDidLoad]; 
} 

访问abov中的myPickerView变量e方法,这将意味着您的UIPickerView对象具有类作用域或属性。

另外,有被按下按钮在这个文件后触发的动作。

-(void)onButtonPress:(id)sender { 
    //This gets the currently selected row from the picker 
    //Again, assuming you have your picker as a property or class variable 
    int row = [myPickerView selectedRowInComponent:0]; 

    //In your final version you need to use a condition to decide which view to show. 
    if (row == 0) { 
     AnotherView *anotherViewInstance = [[AnotherView alloc] init]; 

     //Show "anotherView" 
     [self.navigationController pushViewController:anotherViewInstance animated:YES]; 
    } 
    else if (row == 1) { 
     AnotherView2 *anotherView2 = [[AnotherView2 alloc] init]; 

     //Show "anotherView" 
     [self.navigationController pushViewController:anotherView2 animated:YES]; 
    } 
    else if (row == 2) { 
     AnotherView3 *anotherView3 = [[AnotherView3 alloc] init]; 

     //Show "anotherView" 
     [self.navigationController pushViewController:anotherView3 animated:YES]; 
    } 
    //...continue with the rest of your conditions here 
} 

如果使用界面生成器,那么你应该返回类型更改从voidIBAction

SingleComponentAppDelegate.m

现在我假设你的应用程序委托的didFinishLaunchingWithOptions看起来像这样接近尾声:

//.... 
self.viewController = [[SingleComponentPickerViewController alloc] initWithNibName...]; 
self.window.rootViewController = self.viewController;  
[self.window makeKeyAndVisible]; 
return YES; 

你想修改窗口的RootViewController的属性设置到一个导航控制器,其中包含您的自定义视图控制器。这样,您可以轻松地将视图推送到屏幕上并将其弹出。

//... 
//Create your base view, similar (or even identically) to what was done earlier 
SingleComponentPickerViewController *myView = [[SingleComponentPickerViewController alloc] initWithNibName...]; 

//Stick it inside a UINavigationController so you can push and pop views on top of it. 
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:myView]; 

//Assign that to the window's rootViewController, show it, and return 
self.window.rootViewController = nav;  
[self.window makeKeyAndVisible]; 
return YES; 
+0

嗨eitan27,非常感谢你对你的反应,我很新的这一点,您的文章使我明白pickerview的底层好一点。我只是对我应该在哪里添加这些东西感到困惑。我有这些类是:SingleComponentAppDelegate.m,SingleComponentAppDelegate.h,SingleComponentPickerViewController.h,SingleComponentPickerViewController.m,我创建了一个新的观点,如果我按一个叫AnotherView.h和AnotherView.m按钮打开。 – ReYCangri

+0

你写的东西正是我想要做的,我有我的PickerView中的组件,当我选择一个按下按钮时,它会打开一个新页面,如AnotherView,AnotherView1 ... to..AnotherView20等。这些视图中的每一个都是为我的PickerView中的每个组件制作的。下面的代码是我想要按下的按钮的,但我不知道如何将它链接到选定的行,该行将是该页面的第0行。我试着添加你给我的代码,但是它说navigatorController没有定义。非常感谢你的帮助。 – ReYCangri

+1

非常感谢Eitan,我会按照你的指示,尽我所能让它工作。我只是想学习Objective-C,虽然我在C++方面有一些经验,但这与我在编程课程中所做的有点不同。自从我参加这些课程已经有几年了。我感谢你的帮助。谢谢。 – ReYCangri

相关问题