Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

iphone - Dismiss popover using UIbutton

I can't figure out why it doesn't work as it should when I try to dismiss a popover by clicking on a UIButton which itself is on a the popover to be dismissed, my project crashes...

- (IBAction) cancelButton: (id) sender{
//[self dismissPopoverAnimated:YES];
}

Above is my code for my UIButton

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Dont dismiss the popover from within its self. Make a protocol that sends a message to its delegate to then dismiss it. For example, your popover view controller might look like this..

// MyPopoverViewController.h

@protocol MyPopoverDelegate <NSObject>
-(void)didClickCancelButton;
@end

@interface MyPopoverViewController : UIViewController {

}

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

-(IBAction)cancelButton;

@end

// MyPopoverViewController.m
#import "MyPopoverViewController.h"

@implementation MyPopoverViewController

@synthesize delegate;

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

-(IBAction)cancelButton {
   [self.delegate didClickCancelButton];
}

#pragma mark -
#pragma mark Rotation support

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}

#pragma mark -
#pragma mark Memory Management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {   
    [super dealloc];
}


@end

And then you could use..

//  ClassImplementingPopoverController.h

#import <UIKit/UIKit.h>
#import "MyPopoverViewController.h"

@interface ClassImplementingPopoverController : UIViewController <UIPopoverControllerDelegate, MyPopoverDelegate> {

    UIPopoverController *myPopoverController;
}

@property (nonatomic, retain) UIPopoverController *myPopoverController;

@end

//  ClassImplementingPopoverController.m

#import "ClassImplementingPopoverController.h"
#import "MyPopoverViewController.h"


@implementation ClassImplementingPopoverController

@synthesize myPopoverController;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
}

 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations.
     return YES;
 }

#pragma mark -
#pragma mark MyPopover delegate

-(void)didClickCancelButton {
    if ([myPopoverController isPopoverVisible]) {
       [myPopoverController dismissPopoverAnimated:YES];
       [myPopoverController release];
    }
 }

#pragma mark -
#pragma mark UIPopoverController delegate

-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    if (popoverController == myPopoverController) {
        [myPopoverController release];
    }
}

/* Use something like this to create your popover, just make sure you set the delegate to self so you can receive the messages 

        NSLog(@"Displaying Popover!");
        MyPopoverViewController *detailViewController = [[MyPopoverViewController alloc] initWithNibName:@"MyPopoverViewController" bundle:nil];
        [detailViewController setDelegate:self];
        // Pass the selected object to the new view controller.     
        myPopoverController = [[UIPopoverController alloc] initWithContentViewController:detailViewController];
        [detailViewController release];
        myPopoverController.popoverContentSize = CGSizeMake(500.0, 150.0);
        [myPopoverController setDelegate:self];
*/

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
    self.myPopoverController = nil;
}


- (void)dealloc {
    [myPopoverController release];
    [super dealloc];
}

@end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...