Pop-up modal with UITableView on iPhone

I need to open a quick dialog so that the user can select one parameter in a UITableView from a list of about 2-5 elements. The dialog will be modal and will take about 1/2 screen. I go back and forth between how to handle this. Should I subclass UIViewand make it UITableViewDelegateu DataSource?

I would also prefer to express this opinion in IB. Therefore, to display, I would do something similar from my view controller (suppose 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];

The problem is that I am trying to pass the owner: myDialog, which is zero because it was not created ... I could pass the owner: self, but that would make my view controller File Owner, and that’s not how this dialog looks connected to IB.

So, it makes me think that this dialogue wants to be another full-blown UIViewController... But from everything I read, there should be only one UIViewController on the screen, so it bothers me because I could benefit from viewDidLoad, etc. . that come with view controllers ...

Can anyone fix this for me?

+3
source share
1 answer

, ; view . , .

. UIView be UITableViewDelegate, UIViewController be a UITableViewDelegate. , , UITableViewController, iPhone OS 3.x+. .

, . - UINavigationController, "" . , , , .

, :

- (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 :

@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 - :

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

, :

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

    [self.navigationController dismissModalViewControllerAnimated:YES];
}

Apple, .

+7

Source: https://habr.com/ru/post/1738266/


All Articles