相关疑难解决方法(0)

为什么我的NSNotification会多次调用它的观察者?

在App中,我使用了几个viewcontrollers.在一个viewcontroller上,一个观察者初始化如下:

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethod:) name:@"MyNotification" object:nil];
Run Code Online (Sandbox Code Playgroud)

即使NSNotification在初始化之前移除了执行次数,myMethod:也可以通过相应视图控制器上的重复视图量来总结.

为什么会发生这种情况?如何避免myMethod:被调用一次以上.

注意:我通过使用断点确保多次调用postNotification时没有犯错.

编辑:这就是我的postNotification的样子

NSArray * objects = [NSArray arrayWithObjects:[NSNumber numberWithInt:number],someText, nil];
NSArray * keys = [NSArray arrayWithObjects:@"Number",@"Text", nil];
NSDictionary * userInfo = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:self userInfo:userInfo];
Run Code Online (Sandbox Code Playgroud)

编辑:甚至在我订阅了viewwillappear之后:我得到了相同的结果.myMethod:被多次调用.(我重新加载viewcontroller的次数).

-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethod:) name:@"MyNotification" object:nil];
}
Run Code Online (Sandbox Code Playgroud)

编辑:我的生命周期似乎有些问题.ViewDidUnload和dealloc没有被调用,但是viewdiddisappear被调用.

我将Viewcontroller推送到堆栈的方式如下,其中parent是tableview子类(单击此viewcontroller启动的行:

detailScreen * screen = [[detailScreen alloc] initWithContentID:ID andFullContentArray:fullContentIndex andParent:parent];
[self.navigationController pushViewController:screen animated:YES];
Run Code Online (Sandbox Code Playgroud)

解:

将nsnotification移除到viewdiddisappear就可以了.感谢您的指导!

iphone nsnotifications uiviewcontroller nsnotificationcenter ios

25
推荐指数
3
解决办法
2万
查看次数

WatchKit SDK无法从NSUserDefaults检索数据

我想为Apple手表制作一个测试应用程序,您可以在手机上设置一些String,然后它将显示在Apple Watch上.我决定使用NSUserDefaults类来存储这些数据.

在我的iPhone视图控制器中,我有一个方法,它将输入和存储带入本地存储:

- (IBAction)saveInfo:(id)sender {

    NSString *userInput = [myTextField text];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:userInput forKey:@"savedUserInput"];

    [defaults synchronize];

    self.myLabel.text = [defaults stringForKey:@"savedUserInput"];
}
Run Code Online (Sandbox Code Playgroud)

在我的监视接口控制器中,我有一个检索数据并显示它的方法:

- (IBAction)showText2 {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults synchronize];

    self.myLabel2.text = [defaults stringForKey:@"savedUserInput"];
}
Run Code Online (Sandbox Code Playgroud)

除了某些原因,我试图检索的数据显示为null,标签没有更新.

xcode objective-c ios xcode6 watchkit

25
推荐指数
2
解决办法
1万
查看次数

将WCSession与多个ViewController一起使用

我发现了许多问题和许多答案,但没有最后的例子请求:

任何人都可以在Objective C中给出最后一个示例,将WCSession与IOS应用程序和带有多个ViewController的Watch应用程序(WatchOS2)一起使用的最佳做法是什么.

到目前为止我注意到的是以下事实:

1.)在AppDelegate的父(IOS)应用程序中激活WCSession:

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //Any other code you might have

    if ([WCSession isSupported]) {
        self.session = [WCSession defaultSession];
        self.session.delegate = self;
        [self.session activateSession];
    }
}
Run Code Online (Sandbox Code Playgroud)

2.)在WatchOS2侧使用<WCSessionDelegate>.但其余的对我来说完全不清楚!一些答案是通过在传递的字典中指定键来讨论,如:

[session updateApplicationContext:@{@"viewController1": @"item1"} error:&error];
[session updateApplicationContext:@{@"viewController2": @"item2"} error:&error];
Run Code Online (Sandbox Code Playgroud)

其他人正在讨论检索默认会话

WCSession* session = [WCSession defaultSession];
[session updateApplicationContext:applicationDict error:nil];
Run Code Online (Sandbox Code Playgroud)

其他人在谈论不同的队列?"如果有必要,客户有责任派遣到另一个队列.发送回主要队列."

我完全糊涂了.因此,请举例说明如何将WCSession与IOS应用程序和带有多个ViewController的WatchOS2应用程序一起使用.

我需要它用于以下情况(简化):在我的父应用程序中,我测量心率,锻炼时间和卡路里.在Watch应用程序1. ViewController我将在2显示心率和锻炼时间.ViewController我也将显示心率和燃烧的卡路里.

ios watchkit watchos-2

8
推荐指数
1
解决办法
5039
查看次数