在VC之间传输数据

时间:2015-09-22 04:38:14

标签: ios objective-c uiviewcontroller

我想将数据从VC1中的Label(计算结果)传输到VC2中的另一个标签。 我收到错误"不兼容的pointe类型从UILabel分配给NSString _Nullable "在ViewController2.m<

ViewController.h:

 @property (strong, nonatomic) IBOutlet UILabel *ResultVC1;

查看Controller.m:

    - (IBAction)GoToVc2Button:(id)sender {
[self performSegueWithIdentifier:@"SegueToVc2" sender:self];
}
#pragma mark - Data Sending
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"SegueToVC2"]) {
    ViewController2 *secondVC=(ViewController2 *)segue.destinationViewController;
secondVC.ResultFromVc1=self.lab;
}
}

ViewController2.h:

@property (strong, nonatomic) UILabel *labelVC2;
@property (strong, nonatomic) IBOutlet UILabel *ResultFromVc1;

ViewController2.m:

NSLog(@"Result From Vc1 is%@",self.ResultFromVc1);
_labelVC2.text=self.ResultFromVc1;

2 个答案:

答案 0 :(得分:1)

查看导致错误的行的代码。阅读错误。很清楚问题是什么。您正在为UILabel属性分配NSString

_labelVC2.text=self.ResultFromVc1;

应该是:

_labelVC2.text=self.ResultFromVc1.text;

但为什么要将UILabel传递给UILabel?您应该将NSString传递给NSString。然后使用该字符串设置标签的文本。

答案 1 :(得分:1)

In VC2
@property (strong,nonatomic) NSString *strVal;

-(void) ViewDidLoad
{
labelVC2.text=strVal;
}
In VC1

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"SegueToVC2"]) {
    ViewController2 *secondVC=(ViewController2 *)segue.destinationViewController;
secondVC.strVal=@"Hello";
}
}