首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSNotificationCenter澄清

NSNotificationCenter澄清
EN

Stack Overflow用户
提问于 2015-03-19 11:56:32
回答 1查看 174关注 0票数 1

我是iOS开发中的新手。这是我自己学的。我现在有点搞不懂什么是通知中心。我在网上搜索过,也研究过有关web通知中心的情况。

我之所以发布这个问题,仅仅是为了澄清我对nsnotificationcentre的看法。

我正在建设一个项目,在那里我已经采取了四个视图控制器。第一个视图控制器的名称是默认的。现在我把第二视图控制器命名为国家场景,第三视图控制器命名为国家场景,第四视图控制器命名为城市场景。

在国家场景中,我有一个桌子视图,在那里我显示了10个国家的名单。现在,当我点击细胞的时候,它会进入状态场景,在那里我看到了一个桌面视图,在那里我显示了一个10个州的列表,当我点击任何一个单元时,它就进入了城市场景,在那里我看到了一个桌面视图,并显示了一个10个城市的列表。

现在在我的第一个视图控制器上,我有两个文本字段。一个是选择国家,一个是选择城市。

我选择了使用代表的国家。现在我想选择城市。

选择城市就像,

家庭视图控制器->国家视图控制器-状态视图控制器->城市视图控制器。然后,我点击数据的单元格将显示在我的主视图控制器的城市文本框中。

有谁能告诉我怎样才能在nsnotificationcenter做这件事吗?

这是我的家庭视图控制器实现文件

代码语言:javascript
复制
#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

这是我的国家现场执行文件

代码语言:javascript
复制
#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

这是我的国家现场执行文件

代码语言:javascript
复制
#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

这是我的城市场景实现文件

代码语言:javascript
复制
#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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-19 12:17:16

在您的城市场景中,您必须在通知中传递城市名称作为对象,然后必须在第一个视图控制器(即ViewController )中获取对象。

所以在你的城市场景里,就像这样

代码语言:javascript
复制
[[NSNotificationCenter defaultCenter]postNotificationName:@"Updated City Name" object:[self.arrCities objectAtIndex:indexPath.row]];

现在,在您的第一个有文本字段的ViewContrller中,这样做

viewDidLoad:方法中添加观察者

代码语言:javascript
复制
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateCity:) name:@"Updated City Name" object:nil];

现在实现该方法。

代码语言:javascript
复制
- (void)updateCity:(NSNotification *)notification {
    NSString *cityName = notification.object;
    self.txtCity.text = cityName;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29143938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档