我刚刚加入本网站后,非常难以回答我的问题,并且你们在做什么时看起来很神奇。我想为iPhone构建一个简单的介绍应用程序。我遇到的问题是我想使用UIPickerView来选择各种选项,在pickerview中我有大约20个选项。接下来我想要做的是在单击选项时能够打开下一个窗口,我希望在pickerview中为每个选项设置单独的窗口。基本上我有一个品牌,那么当你在pickerview上选择品牌时选择品牌,你就会转向为该品牌制造的正确配件。每个品牌都有独立的配件,这就是为什么我需要每个品牌都有不同的新窗口。我已经有了我的pickerview,我只是不知道如何继续下一步。一旦我通过点击在选取器上选择一个项目,我希望它移动到相应的下一个窗口。预先感谢您的帮助。如何将UIPickerView选项连接到单独的页面
回答
我会假设你的声明时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
}
如果使用界面生成器,那么你应该返回类型更改从void
到IBAction
。
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;
从UIPickerViewDelegate
Protocol Reference:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
- 1. UIPickerView单独页面
- 2. 连接到单独的页面的Joomla快讯项目
- 3. jQuery选项卡vs单独的页面
- 4. 如何将HTML页面连接到MySQL
- 5. 如何将Facebook页面连接到Reddit页面?
- 6. 如何将两个单独的页面加载到两个单独的iframe?
- 7. 如何将选项添加到woo commerce编辑订单页面?
- 8. Qt:如何在单独的线程中将QScriptEngineDebugger连接到QScriptEngine?
- 9. 如何将选项卡添加到dotProject的项目页面?
- 10. 重定向到表单提交后单独的连续页面
- 11. 将SAP连接到.NET的选项
- 12. 如何检查连接到php页面
- 13. UIPickerView连接
- 14. 如何将我的Facebook页面连接到我的wbsite/domain?
- 15. 基于datalist选项连接到其他页面
- 16. 如何连接页面?
- 17. 如何将面板手风琴项目与面板选项卡连接
- 18. 如何将样式连接到Laravel的页面?
- 19. 将数据库连接到Drupal页面
- 20. JQuery UI选项卡:如何直接导航到其他页面的选项卡?
- 21. 如何将我的listview示例项目连接到下一页
- 22. UIPickerView选择链接到UITableView
- 23. VB - 如何将check_change连接到代码后面的复选框
- 24. idTabs(将页面链接到特定的选项卡)
- 25. 将单独文档中的页眉预先添加到页面
- 26. 将选项连接到按钮
- 27. 将对象连接到javafx选项卡
- 28. 将UITableView单元连接到单独的视图控制器
- 29. 如何使用JSP直接写入单独的HTML页面?
- 30. 如何将MainWindow.xib连接到空项目
嗨eitan27,非常感谢你对你的反应,我很新的这一点,您的文章使我明白pickerview的底层好一点。我只是对我应该在哪里添加这些东西感到困惑。我有这些类是:SingleComponentAppDelegate.m,SingleComponentAppDelegate.h,SingleComponentPickerViewController.h,SingleComponentPickerViewController.m,我创建了一个新的观点,如果我按一个叫AnotherView.h和AnotherView.m按钮打开。 – ReYCangri
你写的东西正是我想要做的,我有我的PickerView中的组件,当我选择一个按下按钮时,它会打开一个新页面,如AnotherView,AnotherView1 ... to..AnotherView20等。这些视图中的每一个都是为我的PickerView中的每个组件制作的。下面的代码是我想要按下的按钮的,但我不知道如何将它链接到选定的行,该行将是该页面的第0行。我试着添加你给我的代码,但是它说navigatorController没有定义。非常感谢你的帮助。 – ReYCangri
非常感谢Eitan,我会按照你的指示,尽我所能让它工作。我只是想学习Objective-C,虽然我在C++方面有一些经验,但这与我在编程课程中所做的有点不同。自从我参加这些课程已经有几年了。我感谢你的帮助。谢谢。 – ReYCangri