Objective-C:需要帮助理解为什么在以两种不同方式访问 UIView 时两种方法的反应不同 [英] Objective-C: Help needed to understand why two methods react differently when accessing a UIView in two different ways

查看:26
本文介绍了Objective-C:需要帮助理解为什么在以两种不同方式访问 UIView 时两种方法的反应不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两天的时间里问了三个问题让我感觉很糟糕,但我被困在论坛上搜索我找不到答案所以我希望有人能帮助我 - 我想这都是学习乐趣的一部分!

I feel bad asking three questions in the space of two days but I am stuck and searching the forum I couldn't find the answer so I hope someone can help me - all part of the fun of learning I suppose!

在我的程序中,我有三个视图,它们的顺序是 GridScreen -> GameScreen -> CorrectScreen.在 CorrectScreen 上,我有一个返回 GridScreen 的按钮.

In my program I have three views which go in the order GridScreen -> GameScreen -> CorrectScreen. On the CorrectScreen I have a button which goes back to the GridScreen.

在 GridScreen 上,我有一堆按钮,用户可以按下这些按钮转到 GameScreen.当用户正确回答问题时,他将从 GameScreen 带到 CorrectScreen 进行确认,然后返回 GridScreen.

On the GridScreen I have a bunch of buttons which a user can press to go to the GameScreen. When the user answers the question correctly he is taken from the GameScreen to the CorrectScreen for acknowledgement and then back to the GridScreen.

在上一个问题中,我询问了如何跟踪在 GridScreen 上按下的按钮,以便当我从 CorrectScreen 返回到它时,我可以用勾号替换图标.之前已经解决了这个问题,但这样做又造成了另一个问题.

In a previous question I asked how to track the button that was pressed on the GridScreen so that when I go back to it from the CorrectScreen I can replace the icon with a tick. That was solved earlier but by doing so I've created another problem.

在 CorrectScreen 中,当用户按下按钮返回时,会调用以下两个函数:

In the CorrectScreen, when the user presses the button to go back the following two functions are called:

[self.gridScreen updateUserIcon:buttonThatWasPressed];
[self.gridScreen updatePoints:accumulatedpoints];

updateUserIcon 在哪里:

where updateUserIcon is:

-(void)updateUserIcon:(UIButton *)button
{
UIButton *buttonPressed = button; 
self.button1 = buttonPressed;
[self.button1 setImage:[UIImage imageNamed:@"tick.png"] forState:UIControlStateNormal];
}

和 updatePoints 是:

and updatePoints is:

-(void)updatePoints:(int)points
{
    self.currentPoints.text = [[NSString alloc]initWithFormat:@"Current points: %d", points];
}

其中 button1 是 UIButton,currentPoints 是 UILabel.

where button1 is a UIButton and currentPoints is a UILabel.

现在,当我在调用这两个函数后使用以下代码返回 GridScreen 时,勾号出现在我想要的按钮上,但标签没有正确更新:第一种情况:

Now, when I go back to the GridScreen using the following code after calling the two functions the tick appears on the button I want, but the label does not update correctly: FIRST CASE:

[[[self presentingViewController]presentingViewController] dismissModalViewControllerAnimated:YES];

而如果我使用下一种方式,则根本不会出现勾号,但标签会完美更新:第二种情况:

whereas if I use this next way, the tick does not appear at all, but the label updates perfectly: SECOND CASE:

GridScreen *screen = [[GridScreen alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:screen animated:YES];

(我通常使用第二种情况加载视图).

(I normally load views using the second case).

在第一种情况下,即使我做了以下代码:

In the first case, even if I did the following code:

-(void)updatePoints:(int)points
{
    self.currentPoints.text = @"A";
    NSLog(@"Current Points %@", self.currentPoints.text);
}

我的 NSLog 返回当前点(空).

My NSLog returns Current Points (null).

该解决方案与我返回 GridScreen 的第一种方法有关,我实际上并没有再次加载视图,但是在第二种方法中我做了 - 但我不明白我需要做什么才能获得分数正确更新并打勾.

The solution is something to do with my first method of going back to the GridScreen I don't actually load the view again but in the second method I do - but I cannot understand what I need to do to get both the score updating correctly and the tick.

如果有人可以提供帮助,我很想知道 - 我对使用 Objective-C 编程还很陌生,所以如果其中任何一个是糟糕的代码",我很高兴被告知出了什么问题,所以我不会继续类似的错误.

If anyone can help I would love to know - I am fairly new to programming with Objective-C so if any of this is 'bad code' I am happy to be told what is wrong so going ahead I don't make similar mistakes.

再次感谢大家,这个网站非常适合提供帮助,提前感谢您的建议.

Thanks again to you all, this site is fantastic for helping out and in advance I appreciate the advice.

安迪.

推荐答案

好吧,愚蠢的问题,但你为什么不直接使用通知来向屏幕发送消息?

OK, silly question, but why aren't you just using Notifications to message the screens?

这是最简单、最安全的方法.

It's the easiest and safest approach.

在 GridScreen 中添加此通知:

In the GridScreen add this notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(toggleLabel:) name:@"CorrectAnswerNotification" object:nil];

在推送新屏幕之前.

现在在 CorrectScreen 中,在弹出视图之前触发该通知:

Now in the CorrectScreen, fire that notification before poping the view:

[[NSNotificationCenter defaultCenter] postNotificationName:@"CorrectAnswerNotification" object:self userInfo:nil];

在 userInfo 字典中传递你想要的任何信息(这可以是一个字典,所以你可以传递任何你需要的信息)

Pass any information you want in the userInfo dictionary (this can be a dictionary so you can pass along any information's you need)

并在 GridScreen 中管理 toggleLabel 方法中的所有内容:

And in the GridScreen manage everything in the toggleLabel method:

-(void)toggleLabel:(NSNotification *)notification {

    //read the documentation dictionary
    NSDictionary *infoDictionary = [notification userInfo];
    //remove the notification first

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"CorrectAnswerNotification" object:nil];

    //now change the buttons...
    [self updateUserIcon:buttonThatWasPressed];
    [self updatePoints:accumulatedpoints];

}

因为我被问及通知中心,这里有一些直接来自 Apple 文档:

Because I was asked about the Notification Center, here are some details straight from Apple Documentation:

一个 NSNotificationCenter 对象(或简称为通知中心)提供一种在节目内广播信息的机制.一个NSNotificationCenter 对象本质上是一个通知调度表.

An NSNotificationCenter object (or simply, notification center) provides a mechanism for broadcasting information within a program. An NSNotificationCenter object is essentially a notification dispatch table.

对象在通知中心注册以接收通知(NSNotification 对象) 使用 addObserver:selector:name:object:或 addObserverForName:object:queue:usingBlock: 方法.每个调用此方法指定一组通知.所以,对象可以通过以下方式注册为不同通知集的观察者多次调用这些方法.

Objects register with a notification center to receive notifications (NSNotification objects) using the addObserver:selector:name:object: or addObserverForName:object:queue:usingBlock: methods. Each invocation of this method specifies a set of notifications. Therefore, objects may register as observers of different notification sets by calling these methods several times.

当一个对象(称为通知发送者)发布一个通知,它向通知发送一个 NSNotification 对象中央.然后通知中心通知任何观察者该通知符合注册时指定的标准向他们发送指定的通知消息,传递通知作为唯一参数.

When an object (known as the notification sender) posts a notification, it sends an NSNotification object to the notification center. The notification center then notifies any observers for which the notification meets the criteria specified on registration by sending them the specified notification message, passing the notification as the sole argument.

通知中心维护一个通知调度表,该表为特定观察者指定通知集.通知set 是发布到通知的通知的子集中央.每个表条目包含三个项目:

A notification center maintains a notification dispatch table which specifies a notification set for a particular observer. A notification set is a subset of the notifications posted to the notification center. Each table entry contains three items:

Notification observer: Required. The object to be notified when qualifying notifications are posted to the notification center.

Notification name: Optional. Specifying a name reduces the set of notifications the entry specifies to those that have this name.

Notification sender: Optional. Specifying a sender reduces the set of notifications the entry specifies to those sent by this object.

这篇关于Objective-C:需要帮助理解为什么在以两种不同方式访问 UIView 时两种方法的反应不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆