알림 전송(NSNotificationCenter)
//프로세스:
1.
등록 알림 2.알림 센터, 메시지 알림 보내기 --------------그중name 이름은 절대 잘못 쓰지 마세요. 세 군데에 나타날 거예요.알림 센터 내부의 방법을 실현하고 전송을 실현한다.4단계, 메시지 발송 완료, 제거
코드는 다음과 같습니다.
#import "FirstViewController.h"#import "SecondViewController.h"#import "UIButton+Create.h"@interface FirstViewController ()
{ UILabel * _label;
}@end@implementation FirstViewController- (void)dealloc
{ [_label release]; // , , [[NSNotificationCenter defaultCenter]removeObserver:self name:@"labelTextNotification" object:nil]; [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;
}
- (void)viewDidLoad
{ [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; self.navigationItem.title = @" "; _label = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 200, 30)]; _label.backgroundColor = [UIColor greenColor]; [self.view addSubview:_label]; UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Push" target:self action:@selector(didClickButtonAction)]; [self.view addSubview:button]; // , , ---------- name , 3 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showLabelText:) name:@"labelTextNotification" object:nil];
// Do any additional setup after loading the view.
}
- (void)showLabelText:(NSNotification *)notification
{ // , , id text = notification.object; _label.text = text;
}
- (void)didClickButtonAction
{
SecondViewController * secondVC = [[SecondViewController alloc]init]; [self.navigationController pushViewController:secondVC animated:YES]; [secondVC release];
}
- (void)didClick:(NSString *)text
{ _label.text = text;
}
- (void)didReceiveMemoryWarning
{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.
}@end
#import "SecondViewController.h"#import "FirstViewController.h"#import "UIButton+Create.h"@interface SecondViewController ()
{ UITextField * _textField;//
}@end@implementation SecondViewController- (void)dealloc
{ [_textField release]; [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;
}
- (void)viewDidLoad
{ [super viewDidLoad]; self.view.backgroundColor = [UIColor orangeColor]; self.navigationItem.title = @" ";
_textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 200, 30)]; _textField.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:_textField]; UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Back" target:self action:@selector(didClickButtonAction)]; [self.view addSubview:button];
// Do any additional setup after loading the view.
}
- (void)didClickButtonAction
{ // [[NSNotificationCenter defaultCenter]postNotificationName:@"labelTextNotification" object:_textField.text]; [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.
}@end
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Access Request, Session and Application in Struts2If we want to use request, Session and application in JSP, what should we do? We can obtain Map type objects such as Req...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.