访问在别处创建的对象的属性

时间:2013-11-17 22:02:35

标签: ios objective-c cocoa-touch uiviewcontroller

在Notes.h中:

#import <Foundation/Foundation.h>

@interface Notes : NSObject

@property(nonatomic, copy) NSString *Title;
@property(nonatomic, copy) NSString *Text;
@property(nonatomic, copy) NSString *Date;

-(void)setTitle:(NSString *)Title andText:(NSString *)Text andDate:(NSString *)Date;

@end
Notes.m中的

#import "Notes.h"

@implementation Notes

@synthesize Title = _Title;
@synthesize Text = _Text;
@synthesize Date = _Date;

-(NSString *)description {
    return [NSString stringWithFormat:@"Title: %@ Text: %@ Date: %@", self.Title,     self.Text, self.Date];
}


-(void)setTitle:(NSString *)Title andText:(NSString *)Text andDate:(NSString *)Date{

    self.Title = Title;
    self.Text = Text;
    self.Date = Date;

}

我有两个View控制器:

在我的按钮中的SecondViewController中

Notes *newNote = [[Notes alloc]init];
[newNote setTitle:self.navigationItem.title andText:noteField.text andDate:dateLabel.text];

设置值可以正常工作,但我如何在FirstViewController中“获取”这些值?

1 个答案:

答案 0 :(得分:0)

在ViewController2.h中,创建属性以保存您将传递的数据:

@property(nonatomic, copy) NSString *Title2;
@property(nonatomic, copy) NSString *Text2;
@property(nonatomic, copy) NSString *Date2;

按下新视图控制器时,在实例化时传递数据:

Notes.m

-(void)methodThatPushesTheNewController{

    ViewController2 *vc2 = [self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
    vc2.Title2 = self.Title;
    vc2.Date2 = self.Date;
    [self.navigationController pushViewController:vc2 animated:YES];
}

注意:请务必在Notes.m中导入viewController2.h。