弹出式模式与iPhone上的UITableView [英] Pop-up modal with UITableView on iPhone

查看:151
本文介绍了弹出式模式与iPhone上的UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要弹出一个快速对话框,让用户从大约2-5个项目的列表中选择UITableView中的一个选项。对话框将是模式的,只占用屏幕的大约1/2。我在如何处理这个之间来回。我应该将 UIView 子类化,并使其成为 UITableViewDelegate DataSource

I need to pop up a quick dialog for the user to select one option in a UITableView from a list of roughly 2-5 items. Dialog will be modal and only take up about 1/2 of screen. I go back and forth between how to handle this. Should I subclass UIView and make it a UITableViewDelegate & DataSource?

我也喜欢在IB中布局这个视图。所以要显示我会从我的视图控制器(假设我有一个属性在我的视图控制器中的 DialogView * myDialog; )这样的事情

I'd also prefer to lay out this view in IB. So to display I'd do something like this from my view controller (assume I have a property in my view controller for DialogView *myDialog;)

NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"DialogView" owner:myDialog options:nil];
myDialog = [nibViews objectAtIndex:0];
[self.view addSubview:myDialog];

问题是我试图传递所有者:myDialog是nil,因为它没有被实例化...我可以传递所有者:self,但这将使我的视图控制器文件的所有者,这不是该对话框视图如何连接在IB。

problem is i'm trying to pass owner:myDialog which is nil as it hasn't been instantiated...i could pass owner:self but that would make my view controller the File's Owner and that's not how that dialog view is wired in IB.

因此,让我认为这个对话框想要成为另一个完整的 UIViewController ...但是,从所有我已经阅读你应该只有一个UIViewController每个屏幕,所以这让我困惑,因为我可以从 viewDidLoad 等随同视图控制器...

So that leads me to think this dialog wants to be another full blown UIViewController... But, from all I've read you should only have ONE UIViewController per screen so this confuses me because I could benefit from viewDidLoad, etc. that come along with view controllers...

有人可以为我整理这个吗?

Can someone please straighten this out for me?

推荐答案

作为视图控制器在屏幕上的东西;其视图在屏幕上。这样,你可以在屏幕上一次显示尽可能多的视图。

There is no such thing as a view controller being on the screen; its view is on the screen. With that said, you can present as many views as you want on the screen at once.

我将创建一个新的视图和视图控制器。您不会使 UIView UITableViewDelegate ,您会创建一个 UIViewController UITableViewDelegate 。但是,如果你使用的是iPhone OS 3.x +,而不是手动执行,而是让你的新视图控制器成为 UITableViewController 的子类。然后您可以以模态方式呈现此视图控制器。

I would create a new view and view controller. You would not make a UIView be a UITableViewDelegate, you make a UIViewController be a UITableViewDelegate. But instead of doing that manually, instead make your new view controller a subclass of UITableViewController, if you're using iPhone OS 3.x+. You can then present this view controller modally.

您可能希望为用户提供取消选择的机会。一个好的方法是将新的对话框视图控制器包装在 UINavigationController 中,然后在导航栏中放置一个取消按钮。然后使用委托模式通知父视图控制器用户已经做出了他们的选择,所以你可以弹出堆栈。

You probably want to give the user a chance to cancel out of the selection. A good way to do that is to wrap your new dialog view controller in a UINavigationController and then put a "Cancel" button in the nav bar. Then use the delegate pattern to inform the parent view controller that the user has made their choice so you can pop the stack.

这里的代码将看起来像你的父视图控制器,当你想呈现此选项对话框:

Here's what the code will look like inside your parent view controller, when you want to present this option dialog:

- (void)showOptionView
{
    OptionViewController* optionViewController = [[OptionViewController alloc] initWithNibName:@"OptionView" bundle:nil];
    optionViewController.delegate = self;
    UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:optionViewController];
    [self.navigationController presentModalViewController:navController animated:YES];
    [navController release];
    [optionViewController release];
}

您的OptionViewController .h将如下所示:

Your OptionViewController .h will look like this:

@protocol OptionViewControllerDelegate;

@interface OptionViewController : UITableViewController
{
    id<OptionViewControllerDelegate> delegate;
}

@property (nonatomic, assign) id<OptionViewControllerDelegate> delegate;

@end

@protocol OptionViewControllerDelegate <NSObject>
- (void)OptionViewController:(OptionViewController*)OptionViewController didFinishWithSelection:(NSString*)selection;
// or maybe
- (void)OptionViewController:(OptionViewController*)OptionViewController didFinishWithSelection:(NSUInteger)selection;
// etc.
@end

您的OptionViewController.m会有一些东西像这样:

Your OptionViewController.m will have something like this:

- (void)madeSelection:(NSUInteger)selection
{
    [delegate OptionViewController:self didFinishWithSelection:selection];
}

在您的原始视图控制器中有一个匹配的方法,如:

Which has a matching method back in your original view controller like:

- (void)OptionViewController:(OptionViewController*)OptionViewController didFinishWithSelection:(NSUInteger)selection
{
    // Do something with selection here

    [self.navigationController dismissModalViewControllerAnimated:YES];
}

Apple的示例源代码中有很多例子遵循这个一般模式。

There are plenty of examples throughout Apple's sample source code that follow this general pattern.

这篇关于弹出式模式与iPhone上的UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆