简体   繁体   English

UIButton关闭了UIView

[英]UIButton closes UIView

How can I dismiss a UIView that is over the main content? 我如何解雇超过主要内容的UIView? 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 这就是你所做的,你创建并初始化了一个名为settingsPopover的新UIView

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. 然后你要删除另一个名为_settingsPopover视图,该视图与showUserSettings方法中本地创建的视图没有关联。

I assume you have created a global variable of type UIView called _settingsPopover . 我假设您已经创建了一个名为_settingsPopover UIView类型的全局变量。 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. 如果是这种情况,那么应该在你的-(void)showUserSettings方法中添加一行并更改另一行来修复它。 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. 这样,您在showUserSettings方法中本地创建的名为settingsPopover新创建的类型视图变量将被复制到名为_settingsPopover变量的全局变量中,这就是您要添加为子视图的变量。

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]; 这样你可以在以后想要从你正在使用的[_settingsPopover removeFromSuperview];删除它[_settingsPopover removeFromSuperview];时引用它[_settingsPopover removeFromSuperview]; in your closeUserSettings method. 在您的closeUserSettings方法中。 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. 这是一个关于在iOS应用程序中实现Splash屏幕的好教程

Implementation with custom splash screen timer: 使用自定义启动画面计时器实现

1) Create a new view controller. 1)创建一个新的视图控制器。 Let's name it SplashViewController that inherites from UIViewController . 让我们将其命名为从UIViewController继承的SplashViewController You don't need to implement anything in the interface and implementation class. 您不需要在接口和实现类中实现任何内容。

2) Create a new nib file. 2)创建一个新的nib文件。 Let's name it SplashView . 我们将其命名为SplashView Open it with the Interface Builder and add a ImageView to the view. 使用Interface Builder打开它并将ImageView添加到视图中。 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: 3)现在,让我们首先在app delegate接口类中添加一些代码,所以它看起来像下面的例子:

@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. 不要忘记使用@class导入SplashViewController。

4) Next, synthesize the splashViewController variable in your implementation class and extend the class by the following code: 4)接下来,在您的实现类中合成splashViewController变量,并通过以下代码扩展该类:

-(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. 初始化splash视图控制器并将其作为子视图添加到主窗口。 Define your desired splash screen time with scheduledTimerWithTimeInterval:2.0f (2.0 is 2 seconds). 使用scheduledTimerWithTimeInterval定义所需的启动画面时间:2.0f(2.0为2秒)。 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. 定时器到期时调用onSlashScreenDone方法。 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. 我在我的应用程序中使用过它。如果需要更多信息,请告诉我。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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