有人可以看看我出错了吗?
我有一个名为Object的对象,它有一个NSString属性。
然后我有一个带有按钮的MainViewController。按钮以编程方式创建,并在运行时添加。
当你点击这个按钮时,它应该将对象的值设置为String,并将其传递给下一个ViewController。
我设法设置Object属性的值,但是当我调用该值来传递它时,它被设置为null。我尝试了很多不同的解决方案,但似乎没有任何效果。谁能看到我的问题所在?我已在下面添加了所有相关代码
Object.h
@interface Object : NSObject
@property (nonatomic, strong) NSString *content;
Object.m
#import "Object.h"
@implementation Object
@synthesize content;
然后在MianViewController中创建按钮并设置Object属性的值 MainViewController.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,其中标签应设置为Object String的标签 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;
}
当应用程序运行HERE
时,最后屏幕抓取我的NSLog答案 0 :(得分:3)
SecondaryViewController *vc;
应该是
SecondaryViewController *vc = (SecondaryViewController *)segue.destinationViewController;