iOS Window切換

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
    
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    
    return YES;
}

ViewController.m

#import "ViewController.h"
#import "PresentViewController.h"
#import "WindowAA.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor redColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 100, 100, 100);
    [btn setBackgroundColor:[UIColor blueColor]];
    [btn setTitle:@"1111" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(aaa) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UIButton *btnn = [UIButton buttonWithType:UIButtonTypeCustom];
    btnn.frame = CGRectMake(100, 300, 100, 100);
    [btnn setBackgroundColor:[UIColor brownColor]];
    [btnn setTitle:@"2222" forState:UIControlStateNormal];
    [btnn addTarget:self action:@selector(bbb) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnn];
    
}

- (void)aaa{
    ViewController *vc = [[ViewController alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
    
    [WindowAA sharedManager].myWindow.hidden = NO;
    [WindowAA sharedManager].myWindow.windowLevel = UIWindowLevelAlert + 2;
    [[WindowAA sharedManager].myWindow makeKeyAndVisible];
}

- (void)bbb{
    PresentViewController *pVC = [[PresentViewController alloc] init];
    [self presentViewController:pVC animated:YES completion:nil];
}

WindowAA 單例

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface WindowAA : NSObject

@property (nonatomic,strong) UIWindow *myWindow;
@property (nonatomic,strong) UIViewController *myViewController;
@property (nonatomic,strong) UIButton *myBtn;

+ (WindowAA *)sharedManager;
@end

#import "WindowAA.h"

@implementation WindowAA
{
    int _a;
}

+ (WindowAA *)sharedManager{
    static WindowAA *Manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Manager = [[self alloc] init];
    });
    return Manager;
}

- (instancetype)init{
    self.myWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.myWindow.backgroundColor = [UIColor purpleColor];
    
    self.myViewController = [[UIViewController alloc] init];
    self.myViewController.view.backgroundColor = [UIColor yellowColor];
    
    UIView *viewaa = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    viewaa.backgroundColor = [UIColor greenColor];
    [self.myViewController.view addSubview:viewaa];
    
    self.myBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.myBtn.frame = CGRectMake(100, 250, 100, 50);
    self.myBtn.backgroundColor = [UIColor blueColor];
    [self.myBtn addTarget:self action:@selector(aaa) forControlEvents:UIControlEventTouchUpInside];
    
    [self.myViewController.view addSubview:self.myBtn];
    
    self.myWindow.rootViewController = self.myViewController;
    _a = 100;
    
    return self;
}

- (void)aaa{
    NSLog(@"------asdad");
    
    [self.myWindow resignKeyWindow];
    self.myWindow.hidden=YES;
    
    UILabel *labe = [[UILabel alloc] initWithFrame:CGRectMake(200, 200 + _a, 100, 50)];
    labe.text = @"asdfasdfasf";
    labe.backgroundColor = [UIColor cyanColor];
    
    _a += 100;
    
    [self.myViewController.view addSubview:labe];
}

@end

PresentViewController.m

#import "PresentViewController.h"
#import "ViewController.h"

@interface PresentViewController ()

@end

@implementation PresentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor blueColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 100, 100, 100);
    [btn setBackgroundColor:[UIColor cyanColor]];
    [btn setTitle:@"333" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(aaa) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)aaa{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

參考

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容