2015-03-19 132 views
1

我是全新的iOS開發人員。我正在自己學習。我現在對什麼是通知中心有點困惑。我通過網絡搜索了關於nsnotification中心的信息。NSNotificationCenter澄清

我發佈這個問題只是因爲澄清我的想法關於nsnotificationcentre。

我正在建立一個項目,我已經採取了四個視圖控制器。第一個視圖控制器的名稱是默認的。現在我已經將第二個視圖控制器命名爲鄉村場景,將第三個視圖控制器命名爲州場景,將第四個視圖控制器命名爲城市場景

在鄉村場景中,我有一個表格視圖,其中顯示了10個國家/地區的列表。現在,當我點擊單元格時,它會進入狀態場景,在該狀態下,我已經看到了一個表格視圖,並且在那裏顯示了10個狀態的列表,當我點擊任何單元格時,它會轉到城市場景,在該場景中,並顯示10個城市的列表。

現在在我的第一個視圖控制器上,我有兩個文本字段。一個是選擇國家,一個是選擇城市。

我選擇了使用代表的國家。現在我想選擇城市。

選擇城市就會像,

首頁視圖控制器--->國家視圖控制器--->狀態視圖控制器--->城市視圖控制器。然後,我點擊該數據的單元格將顯示在我的主視圖控制器的城市文本框中。

任何人都可以告訴我如何使用nsnotificationcenter來做到這一點?

這是我的家 - 視圖 - 控制器實現文件

#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 
+0

iPeter你真的應該讀http://stackoverflow.com/help/how-to-ask – 2015-03-19 11:59:37

+0

對不起! :(我是全新的。:( – iPeter 2015-03-19 12:07:55

回答

1

那麼在你的城市場景中,你必須通過城市名稱作爲通知的對象,然後你必須讓物體在你的第一個視圖控制器即視圖控制器

所以,在你的城市場景,不喜歡這

[[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; 
} 
+0

再次感謝@Sunelwala – iPeter 2015-03-19 12:29:19

+0

我很高興:)! – 2015-03-19 12:30:37