xcode 关闭 Modal Segue 后更新 UIViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11976881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:14:33  来源:igfitidea点击:

Update UIViewController after Dismissing Modal Segue

iosxcodeuiviewcontrolleruilabelmodalviewcontroller

提问by felix_xiao

I am currently designing the structure for my first iPhone game and ran into a problem. Currently, I have a 'MenuViewController' that allows you to pick the level to play and a 'LevelViewController' where the level is played.

我目前正在为我的第一个 iPhone 游戏设计结构并遇到了一个问题。目前,我有一个“MenuViewController”可以让您选择要播放的关卡,还有一个“LevelViewController”可以播放关卡。

A UIButtonon the 'MenuViewController' triggers a modal segue to the 'LevelViewController'.

UIButton“MenuViewController”上的A触发“LevelViewController”的模态转场。

A UIButtonon the 'LevelViewController' triggers the following method to return to the 'MenuViewController':

UIButton'LevelViewController' 上的A触发以下方法返回到 'MenuViewController':

-(IBAction)back:(id)sender //complete
{
    [self dismissModalViewControllerAnimated:YES];
}

The problem is, I have a UILabelon the menu page that prints the number of total points a player has. Whenever I go back to the menu from the level, I want this label to automatically update. Currently, the label is defined programmatically in the 'MenuViewController':

问题是,我UILabel在菜单页面上有一个打印玩家总点数的选项。每当我从关卡返回菜单时,我都希望此标签自动更新。目前,标签是在“MenuViewController”中以编程方式定义的:

-(void)viewDidLoad {
    [super viewDidLoad];
    CGRect pointsFrame = CGRectMake(100,45,120,20);
    UILabel *pointsLabel = [[UILabel alloc] initWithFrame:pointsFrame];
    [pointsLabel setText:[NSString stringWithFormat:@"Points: %i", self.playerPoints]];
    [self.pointsLabel setTag:-100]; //pointsLabel tag is -100 for id purposes
}

self.playerPoints is an integer property of MenuViewController

self.playerPoints 是 MenuViewController 的整数属性

Is there a way I could update the label? Thanks ahead of time!

有没有办法更新标签?提前致谢!

采纳答案by Ariel

Make a property self.pointsLabelthat points to the UILabel, then you can just call something like [self.pointsLabel setText:[NSString stringWithFormat:@"Points: %i", self.playerPoints]];to update the label with the new score

创建一个self.pointsLabel指向 UILabel的属性,然后你可以调用类似的东西[self.pointsLabel setText:[NSString stringWithFormat:@"Points: %i", self.playerPoints]];来用新的分数更新标签

回答by LJ Wilson

This is a perfect case for delegation. When the LevelViewController is done, it needs to fire off a delegate method which is handled in the MenuViewController. This delegate method should dismiss the modal VC and then do whatever else you need it to do. The presenting VC should normally handled the dismissal of modal views it presents.

这是一个完美的委托案例。当 LevelViewController 完成后,它需要触发一个在 MenuViewController 中处理的委托方法。这个委托方法应该关闭模态 VC,然后做任何你需要它做的事情。呈现的 VC 通常应该处理它呈现的模态视图的解除。

Here is a basic example of how to implement this:

这是如何实现这一点的基本示例:

LevelViewController.h (Above the Interface declaration):

LevelViewController.h(在接口声明之上):

@protocol LevelViewControllerDelegate
    -(void)finishedDoingMyThing:(NSString *)labelString;
@end

Same file inside ivar section:

ivar 部分中的相同文件:

__unsafe_unretained id <LevelViewControllerDelegate> _delegate;

Same File below ivar section:

ivar 部分下面的相同文件:

@property (nonatomic, assign) id <LevelViewControllerDelegate> delegate;

In LevelViewController.m file:

在 LevelViewController.m 文件中:

@synthesize delegate = _delegate;

Now in the MenuViewController.h, #import "LevelViewController.h"and declare yourself as a delegate for the LevelViewControllerDelegate:

现在在 MenuViewController.h 中,将#import "LevelViewController.h"自己声明为 LevelViewControllerDelegate 的委托:

@interface MenuViewController : UIViewController <LevelViewControllerDelegate>

Now inside MenuViewController.m implement the delegate method:

现在在 MenuViewController.m 中实现委托方法:

-(void)finishedDoingMyThing:(NSString *)labelString {
    [self dismissModalViewControllerAnimated:YES];
    self.pointsLabel.text = labelString;
}

And then make sure to set yourself as the delegate for the LevelViewController before presenting the modal VC:

然后确保在呈现模态 VC 之前将自己设置为 LevelViewController 的代理:

lvc.delegate = self;  // Or whatever you have called your instance of LevelViewController

Lastly, when you are done with what you need to do inside the LevelViewController just call this:

最后,当你完成了你需要在 LevelViewController 中做的事情时,只需调用它:

[_delegate finishedDoingMyThing:@"MyStringToPassBack"];

If this doesn't make sense, holler and I can try to help you understand.

如果这没有意义,holler 和我可以尝试帮助您理解。

回答by Bee

In your modal view header file, add the property:

在您的模态视图头文件中,添加属性:

@property (nonatomic,assign) BOOL updated;

Then in your main view controller, use didViewAppear with something like:

然后在你的主视图控制器中,使用 didViewAppear 和类似的东西:

-(void)viewDidAppear:(BOOL)animated{
    if (modalView.updated == YES) {
        // Do stuff
        modalView.updated = NO;
    }
}

Where "modalView" is the name of that UIViewController that you probably alloc/init there.

其中“modalView”是您可能在那里分配/初始化的那个 UIViewController 的名称。

Add more properties if you want to pass more info, like what level the user picked.

如果您想传递更多信息,例如用户选择的级别,请添加更多属性。