【问题标题】:Changing the value of a property of another UIViewController更改另一个 UIViewController 的属性值
【发布时间】:2013-06-28 03:02:11
【问题描述】:

我是 iOS 开发的新手,我有点难过。如果有人可以帮助我,那就太好了。

我有一个带有一堆按钮的 UIViewController。这些按钮应该为第二个 UIViewcontroller 设置一个值,具体取决于您按下的按钮。例如,第二个 UIViewController 有一个类似 NSString 的属性。当我在第一个视图控制器中按下一个按钮(比如说“香蕉”)时,我希望第二个 UIViewController 的 NSString 分配给它“香蕉”。或者当我按下名为 Apple 的按钮时,NSString 现在被分配为“Apple”。

在 UI 中,用户将访问第一个控制器以选择按钮,然后访问第二个控制器,该控制器已经具有用户从第一个控制器按下的值,准备进行操作。两个控制器没有通过segue连接。

我希望这很清楚。我只想知道第一个 UIViewController 中的语法,以将值分配给另一个 UIViewController 属性,如 NSString。非常感谢任何帮助。

【问题讨论】:

  • 您在使用 StoryBoard 并希望通过 segue 传递数据吗?如果是的话,有很多这样的问题和大量的答案......
  • 正如我在问题中提到的,两个控制器没有通过 segue 连接。

标签: ios objective-c


【解决方案1】:

您可以让 SecondViewController 成为 FirstViewController 的一个属性。这样 FirstViewController 可以引用它。

编辑在评论中回答问题

属性示例:

@interface FirstViewController()
property (strong, nonatomic) SecondViewController *secondViewController;
@end

@implementation FirstViewController

-(id)init
{
    if (self = [super init]) {
        self.secondViewController = [[SecondViewController alloc] init];
    }
    return self;
}

-(void)didPressBanana
{
    [self.secondViewController bananaWasPressed];

    // OR

    self.secondViewController.fruit = @"banana";
}

@end

SecondViewController.h

@interface SecondViewController

@property (strong, nonatomic) NSString *fruit;

@end

SecondViewController.m

-(void)bananaWasPressed
{
    self.fruit = @"banana";
}

或者,如果您希望采用另一种方式并让 SecondViewController 与 FirstViewController 通信,您也可以使用协议。

协议示例:

SecondViewController.h

@protocol SecondViewControllerDelegate
-(void)didPressBanana;
@end

@interface SecondViewController : UIViewController
    @property (nonatomic, unsafe_unretained) id<SecondViewControllerDelegate> delegate;
@end

SecondViewController.m

-(void)didPressBanana 
{
    [self.delegate didPressBanana];
}

FirstViewController.m

@interface FirstViewController()
@property (nonatomic, strong) SecondViewController *secondViewController;
@end

@implementation FirstViewController <SecondViewControllerDelegate>

-(id)init
{
    if (self = [super init]) {
        self.secondViewController = [[SecondViewController alloc] init];
    }
    return self;
}
-(void)viewDidLoad
{
    [self.secondViewController setDelegate:self];
}

// delegate method conforms to SecondViewControllerDelegate protocol
-(void)didPressBanana
{
    // do something 
}

@end

【讨论】:

  • 感谢您的回复。这看起来像很棒的代码,但只是一个简单的说明:在属性示例中,第一个视图控制器可以为第二个视图控制器中的属性分配一个值吗?那么,如果第二个视图控制器有一个“Fruit” NSString,那么第一个视图控制器可以将“Banana”分配给那个 NSString 吗?更重要的是,如果用户打开第二个视图控制器,它的 Fruit NSString 会是香蕉吗?
  • 对不起,这个问题太长了,我只是想确保我做对了:)
  • 是的,如果 Fruit 属性是在 SecondViewController 的 .h 文件中定义的,或者您可以在 SecondViewController 的bananaWasPressed 方法中进行赋值。我将更新我的帖子以反映此功能。
  • 我在帖子中添加了代码来解决此问题。同样对于您的问题“如果用户打开第二个视图控制器,它会将香蕉作为其 Fruit NSString 吗?”:是!
  • 非常感谢!感谢您的帮助。
【解决方案2】:

如果第一个 UIViewController 没有对第二个 UIViewController 的引用,那么您可以使用由第一个 UIViewController 更新并由第二个 UIViewController 读取的单例模型对象。这是创建单例的一个很好的参考:http://www.cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html

【讨论】:

    【解决方案3】:

    要么将secondViewController 设为FirstViewController 的父视图控制器,在这种情况下,您可以访问父视图控制器的所有属性或使用NSNotificationCenter 将通知从第一视图控制器发送到第二。

    ParentViewController.h

    #import <UIKit/UIKit.h>
    
    @interface ParentViewController : UIViewController
    
    @property (nonatomic,strong) NSString *mytextView;
    
    @end
    

    ChildViewController.h

    #import "ParentViewController.h"
    
    @interface ChildViewController : ParentViewController
    
    @end
    

    ChildViewController.m

    #import "ChildViewController.h"
    
    @interface ChildViewController ()
    
    @end
    
    @implementation ChildViewController
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        self.mytextView = @"Value";
    }
    

    【讨论】:

    • 不幸的是,我无法设置我的这个特定项目,以便其中一个类成为另一个类的父级。但是,我很欣赏深思熟虑的回应。谢谢!
    • 谢谢!我也会试试的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 2020-02-21
    • 2023-01-06
    • 1970-01-01
    • 2017-12-07
    • 2017-04-24
    • 1970-01-01
    相关资源
    最近更新 更多