- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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 ()
{
BOOL fromCountry;
}
@kết thúc
@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];
}
@kết thúc
这是我的乡村场景实现文件
#import "CountryViewController.h"
#import "StateViewController.h"
#import "ViewController.h"
@interface CountryViewController ()
@property (nonatomic) NSString *lastSelectedCountryName;
@kết thúc
@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];
}
khác
[self performSegueWithIdentifier: @"stateScene" sender: self];
}
@kết thúc
这是我的状态场景实现文件
#import "StateViewController.h"
@interface StateViewController ()
@kết thúc
@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.
}
@kết thúc
这是我的城市场景实现文件
#import "CityViewController.h"
@interface CityViewController ()
@kết thúc
@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.
}
@kết thúc
1 Câu trả lời
在你的城市场景中,你必须将城市名称作为通知中的对象传递,然后你必须在你的第一个 View Controller 中获取对象,即 ViewController
所以在你的城市场景中,这样做
[[NSNotificationCenter defaultCenter]postNotificationName:@"Updated City Name" object:[self.arrCities objectAtIndex:indexPath.row]];
现在在你的第一个有文本字段的 ViewController 中,这样做
Trong của bạnviewDidLoad:
方法中添加观察者
[[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/
现在我正在尝试编写一个函数来在屏幕上出现键盘时向上移动框架。我开始使用 NSNNotificationCenter。我的代码正在运行但不正确。当键盘出现时,我的 formView 向上移动,但是当我开
我想知道是否有可能在应用程序处于暂停状态时从 NSNotificationCenter 运行观察者,如果选择器需要播放音乐。基本上,它在歌曲结束时运行以重置队列。是否可以激活选择器,即使应用程序处于后
应用程序可以在内部使用 NSNotificationCenter让不同的部分相互通信,但是这可以扩展到不同的应用程序可以使用它进行通信吗? 编辑 :很抱歉关于 iOS 与 OSX 的混淆。我只添加了
我正在创建一个必须在 OS X 上作为特权帮助工具运行的服务器。在其中,我尝试使用 NSSocketPort , NSFileHandle ,和NSNotificationCenter在监听套接字上执
我需要在两个不同的控制台应用程序(观察者和客户端)之间进行通信。 在观察者应用程序中,我添加了以下代码: [[NSNotificationCenter defaultCenter] postNotif
我正在使用 NSNotificationCenter 在基于文档的应用程序中发送自定义通知。 基于文档的应用程序可以有许多打开的文档。理想情况下,我希望文档及其子文档仅接收在文档或其子文档中创建的通知
NSNotificationCenter 真的很酷,我们可以用它构建非常可扩展的应用程序。我的问题是:我们是否可以在应用程序中滥用 NSNotificationCenter,即密集使用它,还是它不是一
我一直在研究 NSNotificationCenter,我一直想知道您什么时候会使用自己的自定义通知中心而不是 defaultCenter?这样做的好处是什么? 请原谅我的无知,但似乎仅使用 defa
是平衡“addObserver”的标准: [[NSNotificationCenter defaultCenter] removeObserver:self] 在 viewWillDisappear
我使用以下行添加一个观察者: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFini
当我使用一个时,内存/处理的用途是什么: [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dismis
我从调用多个NSNotifications的所有观察器选择器到没有一个起作用。是否存在我可能已禁用的XCode / Application设置,可能会导致此行为? 这个问题几乎影响到我已经注册为观察员
基本上,我正在尝试创建一个允许用户通过 Google 或 Facebook 登录的应用程序。我已经成功创建了整个登录过程,但是在显示 Facebook 个人资料图像时遇到了一个小问题。因此,当用户选择
我的应用程序中有一个 UITableview Controller ,一个 View Controller ,我正在尝试使用 NSNotificationCenter 将 NSDictionary 从
我是 iOS 开发的新手。我正在自己学习这个。我现在对什么是通知中心有点困惑。我通过网络搜索并研究了 nsnotification 中心。 我发布这个问题只是为了澄清我对 nsnotification
在我的一个类(class)中,我发布了一条通知: [[NSNotificationCenter defaultCenter] postNotificationName:@"ALERTNOTI" obj
我有一个读取条形码的类,当我读取条形码时,我会向 NSNotificationCenter 发送通知,如下所示。 -(void)barcodeData:(NSString *)barcode type
我使用 NSNotificationcenter 来实现连接变化的事件监听器。当用户连接或断开 WiFi 网络时。当应用程序处于暂停模式时,是否可以将此通知发送到应用程序? 必须可以做到这一点,以 S
我正在使用 NSNotificationCenter,监听器使用以下代码订阅: [[NSNotificationCenter defaultCenter] addObserver:self
在我的应用程序中,我需要检测 ImagePicker 是否准备好拍照。我在这里找到了解决方案:How to know if iPhone camera is ready to take picture
Tôi là một lập trình viên xuất sắc, rất giỏi!