我有一个必须发送到另一个viewcontroller的字符串数据 - 在我的例子中是一个tabbarcontroller。我在发送时收到了空数据。 以下是代码段:
ViewController.h
#import <UIKit/UIKit.h>
@class ViewController2;
@interface ViewController1 : UIViewController{
@private
ViewController2 *suiteNo;
}
@property(nonatomic,retain) ViewController2 *viewController2;
-(IBAction)suiteNumber:(UIButton *)sender;
@end
我已在.m文件中声明如下
ViewController1.m
@synthesize viewController2;
//data accessed
self.viewController2.suiteNum = [[sender titleLabel] text];
ViewController2.h
#import <UIKit/UIKit.h>
@interface ViewController2 : UIViewController{
NSString *suiteNum;
}
@property(nonatomic,copy)NSString *suiteNum;
@end
ViewController2.m
@synthesize suiteNum;
//the passed string is used here
NSString *firstURL = [segmentOneURL stringByAppendingString:suiteNum];
我有错误的地方吗?如果我错了请纠正我。谢谢。
答案 0 :(得分:1)
不是在viewcontroller2中创建suiteNum属性,而是在appdelegate中声明suiteNum并将字符串值从viewcontroller1复制到suiteNum
像这样:= AppDelegate * appdelegate1 =(AppDelegate *)[[UIApplication sharedApplication] delegate];appdelegate1.suiteNum = [[sender titleLabel] text];
当viewcontroller2出现时覆盖它的viewwillappear委托为
AppDelegate * appdelegate1 =(AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString * firstURL = appdelegate1.suiteNum;