gpt4 book ai didi

ios - 对象属性设置为空

转载 作者:行者123 更新时间:2023-11-28 20:12:10 25 4
gpt4 key购买 nike

有人可以看看我哪里出了问题吗?

我有一个名为 Object 的对象,它有一个 NSString 属性。

然后我有一个带有按钮的 MainViewController。该按钮以编程方式创建并在运行时添加。

当你点击这个按钮时,它应该将对象本身的值设置为一个字符串,并将其传递给下一个 ViewController。

我设法设置了 Object 属性的值,但是当我调用该值来传递它时,它被设置为 null。我尝试了许多不同的解决方案,但似乎没有任何效果。任何人都可以看到我的问题所在吗?我在下面添加了所有相关代码

对象.h

@interface Object : NSObject
@property (nonatomic, strong) NSString *content;

对象.m

#import "Object.h"
@implementation Object
@synthesize content;

然后在 MianViewController 中创建按钮并设置 Object 属性的值主视图 Controller .h

@interface MainViewController : UIViewController
@property (nonatomic, strong) UIButton *myButton;

MainViewController.m

#import "MainViewController.h"
#import "SecondaryViewController.h"
#import "Object.h"
.
.
@synthesize myButton;

- (void)viewDidLoad
{
[super viewDidLoad];

myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.tag = 1;
myButton.frame = CGRectMake(20, 140, 280.f, 40.f);
UIImage *airButton = [UIImage imageNamed:@"gettingHereByAirButton.png"];
[myButton setBackgroundImage:airButton forState:UIControlStateNormal];
[self.view addSubview:myButton];
[myButton addTarget:self action:@selector(myButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}

- (void) myButtonClicked
{
[self performSegueWithIdentifier:@"mySegue" sender:self];
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(@"Getting Here...");

Object *object = [[Object alloc] init];
object.content = @"This is the content of the Object";

NSLog(@"Content of Object Set To: %@", object.content);

SecondaryViewController *vc;
vc.object = object;

NSLog(@"Object: %@", object);
NSLog(@"Object.content: %@", object.content);
NSLog(@"ViewController.object.content: %@", vc.object.content);

NSLog(@"Just Did Stuff...");
}

SecondaryViewController 的标签应该设置为对象字符串的标签SecondaryViewController.h

#import "Object.h"

@interface SecondaryViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (nonatomic, strong) Object *object;

SecondaryViewController.m

@synthesize object;
@synthesize contentLabel;

- (void)viewDidLoad
{
[super viewDidLoad];

NSLog(@"Content of Object: %@", object.content);

self.contentLabel.text = object.content;
}

最后是应用程序运行时我的 NSLog 的屏幕抓取 HERE

最佳答案

SecondaryViewController *vc;

应该是

SecondaryViewController *vc = (SecondaryViewController *)segue.destinationViewController;

关于ios - 对象属性设置为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19823389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com