简体   繁体   中英

UIButton closes UIView

How can I dismiss a UIView that is over the main content? Think of it as a tour popup window with a close button. Here is the code I am using, but it's not working. No errors, or warnings, just doesn't seem to be doing the trick. Any ideas?

    - (void) showUserSettings {
        UIView *settingsPopover = [[UIView alloc] init];
        settingsPopover.frame = CGRectMake(20, 600, 280, 350);
         [settingsPopover.layer setBackgroundColor:[UIColor colorWithRed:(241/255.0) green:(241/255.0) blue:(241/255.0) alpha:1].CGColor];

        [UIView animateWithDuration:0.35 animations:^{
            settingsPopover.frame =  CGRectMake(20, 130, 280, 350);
            settingsPopover.alpha = 1.0f;
        } completion:^(BOOL finished) {
        }];


        UIButton *closeSettings = [[UIButton alloc] initWithFrame:CGRectMake(245, 0, 35, 40)];
        [closeSettings setBackgroundColor:[UIColor colorWithRed:(212/255.0) green:(121/255.0) blue:(146/255.0) alpha:1]];
        [closeSettings setTitle:@"X" forState:UIControlStateNormal];
        [closeSettings addTarget:self action:@selector(closeSettings) forControlEvents:UIControlEventTouchUpInside];

        [settingsPopover addSubview:closeSettings];
        [self.view addSubview:settingsPopover];
    }

    - (void) closeSettings {
        [UIView animateWithDuration:0.35 animations:^{
        _settingsPopover.frame =  CGRectMake(20, 400, 280, 350);
        _settingsPopover.alpha = 1.0f;
        } completion:^(BOOL finished) {
         [_settingsPopover removeFromSuperview];
        }];
    }

You are trying to remove a view that was locally created but you had no reference to it later on when you tried to remove it from the superview.

This is what you did, you created and init'ed a new UIView called settingsPopover

UIView *settingsPopover = [[UIView alloc] init];    
...
[self.view addSubview:settingsPopover];

And then you're removing another view called _settingsPopover which has no association to your locally created view in the showUserSettings method.

I assume you have created a global variable of type UIView called _settingsPopover . And if that is the case, which it should be then in your -(void)showUserSettings method add one line and change another line to fix it all. Have a look below:

-(void)showUserSettings{
    ...
    [settingsPopover addSubview:closeSettings]; <-- in your method the code up to here is fine

    // add this line before your addsubview code
    _settingsPopover = settingsPopover; //We are copying over your local variable to your global variable

    //and change this line from 'settingsPopover' to '_settingsPopover'
    [self.view addSubview:_settingsPopover]; 

This way your newly created variable of type view called settingsPopover that you created locally in the showUserSettings method will be copied to your global variable called _settingsPopover variable, and thats the variable you want to add as a subview.

This way you can reference to it later on when you want to remove it from the superView which you already are doing with [_settingsPopover removeFromSuperview]; in your closeUserSettings method. All your other code is fine.

What you are doing here can also be termed as "Implementing splash screen" . Splash screen is nothing but a quick visual tutorial for user, intended to give a quick look of the features.

Here is a good tutorial about implementing Splash screen in iOS app.

Implementation with custom splash screen timer:

1) Create a new view controller. Let's name it SplashViewController that inherites from UIViewController . You don't need to implement anything in the interface and implementation class.

2) Create a new nib file. Let's name it SplashView . Open it with the Interface Builder and add a ImageView to the view. Then, assign a image you want to have for the splash screen to the image view (Inspector -> Attributes -> Image View -> Image).

3) Now, let's add some code to the app delegate interface class first, so it looks like the example below:

@class SplashViewController;

@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
     UIWindow *window;
     UINavigationController *navigationController;
     SplashViewController *splashViewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) IBOutlet SplashViewController *splashViewController;
@end

The only thing you have to do is to specify your splash view controller and add a property. Don't forget to import SplashViewController by using @class.

4) Next, synthesize the splashViewController variable in your implementation class and extend the class by the following code:

-(void)applicationDidFinishLaunching:(UIApplication *)application {
     splashViewController = [[SplashViewController alloc] initWithNibName:@"SplashView" bundle:nil];
     [window addSubview:splashViewController.view];
     [window makeKeyAndVisible];
     [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(onSlashScreenDone) userInfo:nil repeats:NO];
}

Initialize the splash view controller and add it as subview to the main window. Define your desired splash screen time with scheduledTimerWithTimeInterval:2.0f (2.0 is 2 seconds). Further, add the following method to the class:

-(void)onSlashScreenDone{
     [splashViewController.view removeFromSuperview];
     [window addSubview:[navigationController view]];
     [window makeKeyAndVisible];
}

The method onSlashScreenDone is called when the timer was expired. It removes the splash view controller and adds the follow up controller as subview and make it visible (in our example, a navigation controller).

I've used it in my app.. Let me know if more info needed. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM