在视图控制器之间传递NSString中的数据 - 目标C

时间:2017-12-29 21:12:04

标签: ios objective-c nsstring

我是iOS开发的新手,并试图掌握目标c。我在视图控制器之间传递信息时遇到问题。我使用整数和NSString玩这个代码并且它工作,所以这告诉我VC相互交谈,问题与变量有关。 dataString由timeString和dateString组成,它们在第一个视图控制器中正常工作。然后当字符串传递给SecondViewController时,它们每个都有空值。在页面的底部,我有NSLog输出。任何人都可以看到我错过的或我做错了什么?

第一个视图控制器.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    NSTimer *time;
    NSString *dateString;
    NSString *timeString;
    int milliseconds;
    int seconds;
    int minutes;
 }
 - (IBAction)startButton:(UIButton *)sender;
 - (IBAction)finishButton:(UIButton *)sender;

 @property (retain) NSString *dataString; //string that will be passed 

 @property (strong, nonatomic) IBOutlet UILabel *timer;
 @property (strong, nonatomic) IBOutlet UIButton *button;
 @property (strong, nonatomic) IBOutlet UIButton *finish;

-(NSString *)getData; //I'm not sure why I need this here
@end 

第一个视图控制器.m

 #import "ViewController.h"
 #import "SecondViewController.h"
 @interface ViewController ();

 @end

 @implementation ViewController

 - (IBAction)startButton:(UIButton *)sender {
     //timer interval increments by milliseconds in updateTimer
     time = [NSTimer scheduledTimerWithTimeInterval:.01 target:self 
         selector:@selector(updateTimer) userInfo:nil repeats:YES];
     NSDate *date = [NSDate date];
     dateString = [self getDate: date]; //saves date and time the button was pressed 
 }

-(void)updateTimer {
    milliseconds += 1;
    if (milliseconds == 99){
        seconds += 1;
        milliseconds = 0;
    }
    if (seconds == 59){
        minutes += 1;
        seconds = 0;
    }
    timeString = [NSString stringWithFormat: @"%02i:%02i.%02i", minutes, seconds, milliseconds]; //formats display time in string
    self.timer.text = timeString;
}

-(NSString *)getDate : (NSDate *)date{
    date = [NSDate date]; //updates object with current date
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMM dd, yyyy hh:mm a"]; //formats date
    dateString = [dateFormatter stringFromDate:date]; //converts date object to string
    return dateString;
}

- (IBAction)finishButton:(UIButton *)sender {
    //button navigates to next view controller
    if (seconds > 0){
        [self getData]; //print current values before trying to pass
    }
    milliseconds = 0; //resets timer
    seconds = 0;
    minutes = 0;
    self.timer.text = @"00:00.00";
}

 //getter method do send string to next view controller 
-(NSString *)getData{
    dataString = [NSString stringWithFormat:@"%@ on %@", timeString, dateString]; //format data
    NSLog(@"%@ %@", dateString, timeString); //to double check values
    return dataString;
}

@end

第二视图控制器.h

#import <UIKit/UIKit.h>
#import "ViewController.h"

@interface dataScreenViewController : UIViewController {
    NSString *string; //string that I'm trying to pass
    ViewController *information //object for view controller class 
}
- (IBAction)press:(UIButton *)sender; //button to display string 
@end  

第二个视图控制器.m

#import "SecomdViewController.h"
#import "ViewController.h"

@interface SecondViewController ()

@end

- (void)viewDidLoad {
    [super viewDidLoad];
    information = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
}

- (IBAction)press:(UIButton *)sender {
    string = [information getData];
    NSLog(@"%@", string); //prints the string
} 

NSLog输出:

2017-12-29 12:47:49.792138-0800 MyFirstProject [6659:333408] 2017年12月29日12:47 PM 00:01.50

2017-12-29 12:47:52.499692-0800 MyFirstProject [6659:333408](null)(null)

2017-12-29 12:47:52.499910-0800 MyFirstProject [6659:333408](null)on(null)

由于第一次打印输出具有正确的信息,我假设在第一个VC中一切正常,但是当我传递时,字符串中变量的信息会丢失。

编辑: 以下是ViewControllers相互通信以传递int和字符串的示例。我刚刚在getData方法中进行了更改:

-(NSString *)getData{
    NSString *string = @"This is a string";
    string = [NSString stringWithFormat:@"%i on %@", 3, string];
    return string;
}

然后我仍然在第二个视图控制器中以相同的方式访问它:

- (IBAction)press:(UIButton *)sender {
    NSString *string = [information getData];
    NSLog(@"%@", string); 
}

然后输出是:

2017-12-30 09:07:40.764295-0800 MyFirstProject [8435:460529] 3 on this is a string

1 个答案:

答案 0 :(得分:1)

您的第二个视图控制器会创建一个名为ViewController的新的非活动/死information对象。并且,您不会在information上调用任何初始化其日期变量的方法。

因此information的{​​{1}}和timeString未初始化。 Objective-C中未初始化的变量为dateString。这就是你在输出中看到NULL的原因。

假设您只需要一个(null)对象,您希望在第二个视图控制器中使用该对象。如果是这种情况,您必须将ViewController的引用传递给第二个视图控制器。