[英]UIButton closes UIView
我如何解雇超過主要內容的UIView? 可以將其視為帶有關閉按鈕的巡視彈出窗口。 這是我正在使用的代碼,但它不起作用。 沒有錯誤或警告,似乎沒有做到這一點。 有任何想法嗎?
- (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];
}];
}
您正嘗試刪除本地創建的視圖,但稍后當您嘗試從超級視圖中刪除它時,您沒有引用它。
這就是你所做的,你創建並初始化了一個名為settingsPopover
的新UIView
UIView *settingsPopover = [[UIView alloc] init];
...
[self.view addSubview:settingsPopover];
然后你要刪除另一個名為_settingsPopover
視圖,該視圖與showUserSettings
方法中本地創建的視圖沒有關聯。
我假設您已經創建了一個名為_settingsPopover
UIView
類型的全局變量。 如果是這種情況,那么應該在你的-(void)showUserSettings
方法中添加一行並更改另一行來修復它。 看看下面:
-(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];
這樣,您在showUserSettings
方法中本地創建的名為settingsPopover
新創建的類型視圖變量將被復制到名為_settingsPopover
變量的全局變量中,這就是您要添加為子視圖的變量。
這樣你可以在以后想要從你正在使用的[_settingsPopover removeFromSuperview];
刪除它[_settingsPopover removeFromSuperview];
時引用它[_settingsPopover removeFromSuperview];
在您的closeUserSettings
方法中。 你所有的其他代碼都沒問題。
你在這里做的也可以被稱為“實現啟動畫面” 。 啟動畫面只是一個快速的用戶可視化教程,旨在快速查看功能。
這是一個關於在iOS應用程序中實現Splash屏幕的好教程 。
使用自定義啟動畫面計時器實現
1)創建一個新的視圖控制器。 讓我們將其命名為從UIViewController
繼承的SplashViewController
。 您不需要在接口和實現類中實現任何內容。
2)創建一個新的nib文件。 我們將其命名為SplashView
。 使用Interface Builder打開它並將ImageView添加到視圖中。 然后,將要用於啟動畫面的圖像指定給圖像視圖(檢查器 - >屬性 - >圖像視圖 - >圖像)。
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
您唯一需要做的就是指定啟動視圖控制器並添加屬性。 不要忘記使用@class導入SplashViewController。
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];
}
初始化splash視圖控制器並將其作為子視圖添加到主窗口。 使用scheduledTimerWithTimeInterval定義所需的啟動畫面時間:2.0f(2.0為2秒)。 此外,將以下方法添加到類中:
-(void)onSlashScreenDone{
[splashViewController.view removeFromSuperview];
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
定時器到期時調用onSlashScreenDone
方法。 它刪除了啟動視圖控制器並將跟進控制器添加為子視圖並使其可見(在我們的示例中,是導航控制器)。
我在我的應用程序中使用過它。如果需要更多信息,請告訴我。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.