iphone如何将字符串传递给另一个视图标签?

时间:2011-05-24 08:34:48

标签: iphone uiview nsstring

我的问题是如何将字符串传递给另一个视图标签?我尝试了很多例子,但仍然无法获得我通过的价值。

这是我保存数据。

-(IBAction)Save:(id)sender{
    timedata = [datePicker date];
    NSLog(@"timedata save is = %@",timedata);

    time = [NSString stringWithFormat:@"%@",timedata];

    NSLog(@"String time = %@",time);

    [self dismissModalViewControllerAnimated:YES];
}

这里是我要显示保存数据。

- (void) viewWillAppear:(BOOL)animated {

    show = [[SelectDate alloc]initWithNibName:@"SelectDate" bundle:nil];
    show.time = time;
    NSLog(@"time = %@",time);

    Selectime.text = show.time;
    NSLog(@"show.time = %@",show.time);     

    [super viewWillAppear:animated];
}

3 个答案:

答案 0 :(得分:1)

如果您在SelectDate viewController中设置了time属性,以便可以在其他viewControllers中访问它。

// SelectDate.h

NSString *time;  
// Your declarations  
@property(nonatomic,retain) NSString *time;  

// SelectDate.m

@synthesize time;  

现在,您可以在其他ViewControllers中使用时间,就像您正在做的那样。

答案 1 :(得分:0)

要获取日期的NSString表示,您应该查看NSDateFormatter

示例:

NSDate* timedata = [datePicker date];   
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSString *time = [dateFormat timedata];
[dateFormat release];

答案 2 :(得分:0)

不是访问上一个视图,而是将数据传递到下一个视图:

嗨,我的问题是如何将字符串传递给另一个视图标签?我尝试了很多例子,但仍然无法获得我通过的价值。

这是我保存数据。

-(IBAction)Save:(id)sender{
    timedata = [datePicker date];
    NSLog(@"timedata save is = %@",timedata);
    time = [NSString stringWithFormat:@"%@",timedata];
    NSLog(@"String time = %@",time);
    [self dismissModalViewControllerAnimated:YES];

    YourNextView *yourNextView=[[YourNextView alloc] init];
    yourNextView.anString=[NSString stringWithFormat:@"%@",timedata];
    [yourNextView release];
}

这里是您要显示保存数据。

- (void) viewWillAppear:(BOOL)animated {

    //show = [[SelectDate alloc]initWithNibName:@"SelectDate" bundle:nil];
    //show.time = time;
    //NSLog(@"time = %@",time);
    Selectime.text = self.anString;
    NSLog(@"show.time = %@",show.time); 
    [super viewWillAppear:animated];
}