gpt4 book ai didi

ios - NSNotificationCenter 说明

转载 作者:行者123 更新时间:2023-11-29 12:25:58 28 4
gpt4 key购买 nike

我是 iOS 开发的新手。我正在自己学习这个。我现在对什么是通知中心有点困惑。我通过网络搜索并研究了 nsnotification 中心。

我发布这个问题只是为了澄清我对 nsnotificationcentre 的看法。

我正在构建一个项目,其中我使用了四个 View Controller 。第一个 View Controller 的名称是默认的。现在我将第二个 View Controller 命名为乡村场景,第三个 View Controller 命名为州场景,第四个 View Controller 命名为城市场景。

在国家场景中,我有一个表格 View ,其中显示了 10 个国家/地区的列表。现在,当我点击单元格时,它会转到我拍摄表格 View 的州场景,并在那里显示 10 个州的列表,当我点击任何单元格时,它会转到我拍摄表格 View 的城市场景并显示 10 个城市的列表。

现在在我的第一个 View Controller 上,我有两个文本字段。一种用于选择国家,一种用于选择城市。

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

选择城市会像这样,

主页 View Controller ---> 国家 View Controller ---> 州 View Controller ---> 城市 View Controller 。然后我点击该数据的单元格将显示在我的主视图 Controller 的城市文本字段中。

谁能告诉我如何使用 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

这是我的状态场景实现文件

#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

最佳答案

在你的城市场景中,你必须将城市名称作为通知中的对象传递,然后你必须在你的第一个 View Controller 中获取对象,即 ViewController

所以在你的城市场景中,这样做

[[NSNotificationCenter defaultCenter]postNotificationName:@"Updated City Name" object:[self.arrCities objectAtIndex:indexPath.row]];

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

在您的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;
}

关于ios - NSNotificationCenter 说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29143938/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com