我是iOS开发的新手。我自己学习这个。我现在对什么是通知中心有点困惑。我通过网络搜索并研究了nsnotification中心。
我发布这个问题只是为了澄清我对nsnotificationcentre的看法。
我正在构建一个项目,我已经采用了四个视图控制器。第一个视图控制器的名称是默认的。现在我将第二个视图控制器命名为国家场景,第三个视图控制器命名为状态场景,第四个视图控制器命名为城市场景。
在乡村场景中,我有一个表格视图,其中显示了10个国家/地区的列表。现在,当我点击单元格时,它进入状态场景,我已经在桌面视图中显示了10个状态的列表,当我点击任何单元格时,它会进入城市场景,我已经在桌面视图中并显示了10个城市的列表。
现在在我的第一个视图控制器上,我有两个文本字段。一个是选择国家,一个是选择城市。
我选择了使用代表的国家/地区。现在我想选择城市。
选择城市就好了,
家庭视图控制器--->国家视图控制器--->状态视图控制器--->城市景观控制器。然后我点击该数据的单元格将显示在我的主视图控制器的城市文本字段中。
任何人都可以告诉我如何使用nsnotificationcenter进行此操作?
这是我的Home View Controller实施文件
#import "ViewController.h"
#import "CountryViewController.h"
@interface ViewController ()<countryDelegate>
{
BOOL fromCountry;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.txtCity.delegate = self;
self.txtCountry.delegate = self;
self.txtCountry.text = self.strname;
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateCityName:) name:@"Updated City Name" object:nil];
}
-(void)updateCity:(NSString *)city
{
self.txtCity.text = city;
}
-(void) dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"Updated City Name" object:nil];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField.tag == 1) {
fromCountry=YES;
}
else{
[self performSegueWithIdentifier:@"countryScene" sender:self];
}
return YES;
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
[_txtCountry resignFirstResponder];
CountryViewController *detailObject = (CountryViewController *) segue.destinationViewController;
detailObject.delegate=self;
detailObject.isFromCountry = fromCountry;
}
-(void)updateCountry:(NSString *)country
{
_txtCountry.text=country;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)countryButtonClicked:(id)sender {
fromCountry=YES;
[self performSegueWithIdentifier:@"countryScene" sender:self];
}
@end
这是我的国家/地区场景实施文件
#import "CountryViewController.h"
#import "StateViewController.h"
#import "ViewController.h"
@interface CountryViewController ()
@property (nonatomic) NSString *lastSelectedCountryName;
@end
@implementation CountryViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.arrCountries = @[@"India", @"Bangladesh", @"Australia", @"New Zealand", @"South Africa", @"West Indies", @"Sri Lanka", @"England", @"Argentina", @"Brazil"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"countryCell"];
UILabel *lblCountry = (UILabel*)[cell.contentView viewWithTag:3];
lblCountry.text = [self.arrCountries objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.arrCountries count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_isFromCountry) {
if ([self.delegate respondsToSelector:@selector(updateCountry:)])
{
[self.delegate updateCountry:[_arrCountries objectAtIndex:indexPath.row]];
}
[self.navigationController popViewControllerAnimated:YES];
}
else
[self performSegueWithIdentifier: @"stateScene" sender: self];
}
@end
这是我的State Scene实现文件
#import "StateViewController.h"
@interface StateViewController ()
@end
@implementation StateViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.arrStates = @[@"West Bengal", @"Uttar Pradesh", @"Madhya Pradesh", @"Jharkhand", @"Bihar", @"Tamilnadu", @"Myanmar", @"Arunachal Pradesh", @"Assam", @"Goa"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"stateCell"];
UILabel *lblStates = (UILabel*)[cell.contentView viewWithTag:4];
lblStates.text = [self.arrStates objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.arrStates count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier: @"cityScene" sender: self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
这是我的城市场景实施文件
#import "CityViewController.h"
@interface CityViewController ()
@end
@implementation CityViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.arrCities = @[@"Kolkata", @"Bangalore", @"Chennai", @"Mumbai", @"Hyderabad", @"Mangalore", @"New York", @"London", @"Rio de Janeiro", @"Buenos Aires"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cityCell"];
UILabel *lblCities = (UILabel*)[cell.contentView viewWithTag:5];
lblCities.text = [self.arrCities objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.arrCities count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[[NSNotificationCenter defaultCenter]postNotificationName:@"Updated City Name" object:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 0 :(得分:1)
在您的城市场景中,您必须将城市名称作为通知中的对象传递,然后您必须在第一个视图控制器中获取对象,即ViewController
所以在您的城市场景中,请这样做
[[NSNotificationCenter defaultCenter]postNotificationName:@"Updated City Name" object:[self.arrCities objectAtIndex:indexPath.row]];
现在,在您的第一个ViewContrller中,您有文本字段,请执行此操作
在viewDidLoad:
方法
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateCity:) name:@"Updated City Name" object:nil];
现在实施方法
- (void)updateCity:(NSNotification *)notification {
NSString *cityName = notification.object;
self.txtCity.text = cityName;
}