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];
}