你可以在一个界面发送通知,比如说在button的点击事件里面。当点击之后你想弹出视图。
但是你这个button并不是在控制器里直接写的。这时候就可以使用。
发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"ttnew" object:nil];
另一个界面接受通知,并且要对应。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ttNewAction:) name:@"ttnew" object:nil];
接受通知时先删除,放置视图多次弹出。写的时候就遇到了这个问题。
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ttnew" object:nil];
实现通知方法。
最后移除
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ttnew" object:nil];
}
完成 。。。。nice。下面是具体的代码。
```
//提交按钮
//footer
UIView *footerview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
footerview.backgroundColor = [UIColor groupTableViewBackgroundColor];
UIButton *tijiaoBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
tijiaoBtn.frame = CGRectMake(30, 200/ 2 - 30, SCREEN_WIDTH - 60, 60);
[tijiaoBtn setTitle:@"提交信息" forState:(UIControlStateNormal)];
[tijiaoBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
tijiaoBtn.layer.cornerRadius = 10;
tijiaoBtn.layer.masksToBounds = YES;
tijiaoBtn.backgroundColor = NavBarColor;
[tijiaoBtn addTarget:self action:@selector(tijiaoAction:) forControlEvents:(UIControlEventTouchUpInside)];
[footerview addSubview:tijiaoBtn];
tableview.tableFooterView = footer view;
//按钮点击事件。通知视图弹出
- (void)tijiaoAction:(UIButton *)sender
{
TTNewViewController *ttnew = [TTNewViewController new];
ttnew.detail = @"是否确认提交认证信息?PS:已经认证将无法修改!";
ttnew.titleStr = @"温馨提示";
[ttnew show];
//点击了确定按钮之后弹出第二个框。。
//先将通知进行删除
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"ttnew" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ttNewAction:) name:@"ttnew" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"fengfeng" object:nil];
}
- (void)ttNewAction:(NSNotification *)note
{
TTViewController 是一个封装的弹出视图。代码在下方。
TTViewController *tt = [TTViewController new];
tt.detail = @"您已提交申请提现,请耐心等待后台审核!";
tt.titleStr = @"温馨提示";
[tt show];
}
最后删除通知。
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"fengfeng" object:nil];
}
.h中的内容。
#import@interface TTViewController : UIViewController
@property (nonatomic, copy) NSString *titleStr, *detail;
- (void)show;
@end
.m中的内容
#import "TTNewViewController.h"
#define TITLE_HEIGHT 46
#define PAYMENT_WIDTH [UIScreen mainScreen].bounds.size.width-80
#define PWD_COUNT 6
#define DOT_WIDTH 10
#define KEYBOARD_HEIGHT 216
#define KEY_VIEW_DISTANCE 100
#define ALERT_HEIGHT 200
@interface TTNewViewController ()
{
UILabel *titleLab, *detailLab, *line1, *line2;
UIButton *cancelBtn;
UIButton *yesBtn;
}
@property (nonatomic, strong) UIWindow *showWindow;
@property (nonatomic, strong) UIView *paymentAlert, *inputView;
@end
@implementation TTNewViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (instancetype)init {
self = [super init];
if (self) {
self.view.frame = [UIScreen mainScreen].bounds;
self.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3f];
[self drawView];
}
return self;
}
- (void)drawView
{
if (!_paymentAlert) {
_paymentAlert = [[UIView alloc]initWithFrame:CGRectMake(40, SCREEN_HEIGHT-526, SCREEN_WIDTH-80, 200)];
_paymentAlert.layer.cornerRadius = 5.f;
_paymentAlert.layer.masksToBounds = YES;
_paymentAlert.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_paymentAlert];
titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-80, 40)];
titleLab.textColor = [UIColor grayColor];
titleLab.textAlignment = NSTextAlignmentCenter;
titleLab.font = [UIFont systemFontOfSize:16];
[_paymentAlert addSubview:titleLab];
//
line1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH - 80, 1)];
line1.backgroundColor = [UIColor blackColor];
[_paymentAlert addSubview:line1];
//
detailLab = [[UILabel alloc] initWithFrame:CGRectMake(40, 50, SCREEN_WIDTH - 80 - 80, 100)];
detailLab.textColor = [UIColor blackColor];
detailLab.font = [UIFont systemFontOfSize:17];
detailLab.textAlignment = NSTextAlignmentCenter;
detailLab.numberOfLines = 0;
[_paymentAlert addSubview:detailLab];
line2 = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(detailLab.frame) + 9, SCREEN_WIDTH - 80, 1)];
line2.backgroundColor = [UIColor blackColor];
[_paymentAlert addSubview:line2];
cancelBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
cancelBtn.frame = CGRectMake(0, CGRectGetMaxY(line2.frame), (SCREEN_WIDTH - 80) /2 -1, 40);
[cancelBtn setTitle:@"取消" forState:(UIControlStateNormal)];
[cancelBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
[cancelBtn addTarget:self action:@selector(cancelBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
[_paymentAlert addSubview:cancelBtn];
//
yesBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
yesBtn.frame = CGRectMake( (SCREEN_WIDTH - 80) / 2 + 1, CGRectGetMaxY(line2.frame), (SCREEN_WIDTH - 80) /2 - 1, 40);
[yesBtn setTitle:@"确定" forState:(UIControlStateNormal)];
[yesBtn setTitleColor:NavBarColor forState:(UIControlStateNormal)];
[yesBtn addTarget:self action:@selector(yesBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
[_paymentAlert addSubview:yesBtn];
UILabel *line3 = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 80) /2 - 1, CGRectGetMaxY(line2.frame), 2, 40)];
line3.backgroundColor = NavBarColor;
[_paymentAlert addSubview:line3];
}
}
//确定按钮
- (void)yesBtnAction:(UIButton *)sender
{
[UIView animateWithDuration:0.3f animations:^{
_paymentAlert.transform = CGAffineTransformMakeScale(1.21f, 1.21f);
_paymentAlert.alpha = 0;
self.showWindow.alpha = 0;
} completion:^(BOOL finished) {
// [self removeFromSuperview];
[self.showWindow removeFromSuperview];
[self.showWindow resignKeyWindow];
self.showWindow = nil;
}];
//发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"ttnew" object:nil];
}
//取消按钮
- (void)cancelBtnAction
{
[UIView animateWithDuration:0.3f animations:^{
_paymentAlert.transform = CGAffineTransformMakeScale(1.21f, 1.21f);
_paymentAlert.alpha = 0;
self.showWindow.alpha = 0;
} completion:^(BOOL finished) {
// [self removeFromSuperview];
[self.showWindow removeFromSuperview];
[self.showWindow resignKeyWindow];
self.showWindow = nil;
}];
}
- (void)show {
// UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
// [keyWindow addSubview:self];
UIWindow *newWindow = [[UIWindow alloc]initWithFrame:self.view.bounds];
// newWindow.backgroundColor = [UIColor clearColor];
newWindow.rootViewController = self;
[newWindow makeKeyAndVisible];
self.showWindow = newWindow;
_paymentAlert.transform = CGAffineTransformMakeScale(1.21f, 1.21f);
_paymentAlert.alpha = 0;
[UIView animateWithDuration:.7f delay:0.f usingSpringWithDamping:.7f initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
_paymentAlert.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
_paymentAlert.alpha = 1.0;
} completion:nil];
}
#pragma mark -
- (void)setTitleStr:(NSString *)titleStr {
_titleStr = titleStr;
titleLab.text = _titleStr;
}
- (void)setDetail:(NSString *)detail {
_detail = detail;
detailLab.text = _detail;
}
```
网友评论