在 iPhone 上使用 UITableView 弹出模式 [英] Pop-up modal with UITableView on iPhone

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

问题描述

我需要弹出一个快速对话框,让用户从大约 2-5 个项目的列表中选择 UITableView 中的一个选项.对话框将是模态的,仅占屏幕的 1/2 左右.我在如何处理这个问题之间来回走动.我应该将 UIView 子类化并使其成为 UITableViewDelegate &数据源?

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];

问题是我试图传递 owner:myDialog 因为它没有被实例化所以它是零...我可以传递 owner:self 但这会使我的视图控制器成为 File's Owner这不是该对话框视图在 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?

推荐答案

屏幕上没有视图控制器;它的 view 在屏幕上.话虽如此,您可以一次在屏幕上显示任意数量的视图.

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天全站免登陆