Code to open a UIPopoverController

99封情书 提交于 2019-12-08 00:01:53

问题


I currently have code to open a UIViewController, in this case you have opened it before so it will not totally load from scratch. So the old data will still be there when you return. I am not trying to save the data for the next time you go in the app. Only in that session. I am trying to adapt this code to the iPad for a UIPopOver. I have tried but am not able to do it.

//without popover

  - (IBAction) addPerson:(id) sender{
if (addPersonController == nil) {
    addPersonController = [[addPersonViewController alloc] initWithNibName:@"addPersonViewController" bundle:nil];
}

addPersonController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:addPersonController animated:YES];
[addPersonController retain];
 }

//popover

  - (IBAction) addPerson:(id) sender{
// create your view controller if it doesn't exist yet
if (dateViewPopOverController == nil){
    addPersonViewController1 = [[addPersonViewControllerPopover_iPad alloc] init];
}
 pop = [[UIPopoverController alloc] initWithContentViewController:addPersonViewController1];

// rest of your method...
addPersonViewController1.delegate = self;

pop.popoverContentSize = CGSizeMake(320, 480);

CGRect rect = CGRectMake(790, 35, 175, 300);


[pop presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];





 }

回答1:


You can keep the dateViewPopOverController as an instance variable in the view controller you are presenting the popover from. That way, when a popover is dismissed, your view controller still holds on to the dateViewPopOverController that was displayed.

in your .h file:

@interface YourViewControllerName : UIViewController {

    dateViewPopOverViewController_iPad *dateViewPopOverViewController;
    // other ivars...

}

@property (nonatomic, retain) dateViewPopOverViewController_iPad *dateViewPopOverViewController;
// other @properties...

in your .m file:

synthesize:

@synthesize dateViewPopoverController;

your method:

- (IBAction) selectStartDate:(id) sender {
    NSLog(@"Select start date");

    // create your view controller if it doesn't exist yet
    if (dateViewPopOverController == nil)
        dateViewPopOverViewController =  
            [[dateViewPopOverViewController_iPad alloc] init];

    popover2 = [[UIPopoverController alloc] 
        initWithContentViewController:dateViewPopOverViewController];

    // rest of your method... *but do not release the dateViewPopOverViewController here*

}

release the controller in dealloc:

- (void) dealloc {

    [dateViewPopOverViewController release];
    // rest of dealloc...

}



回答2:


It looks like you just need an instance variable to hold your dateViewPopOverViewController_iPad instance. Then use the same "if it's nil, create an instance" logic that you had for the DateViewController in the non-popover code.



来源:https://stackoverflow.com/questions/6024822/code-to-open-a-uipopovercontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!