我正在尝试将文本字段数据传递到数组中,然后将其从ViewController2传递到ViewController。在新的视图控制器中,它将打印出数组中的所有元素。
我得到的错误是在ViewController2.m中:'UItextField'没有可见的@interface声明选择器'componentsSeparatedByString:'
我已检查过每个连接,但它们是正确的。
ViewController2.h
IBOutlet UITextField *HomePlayers;
ViewController2.m
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"SaveData"]) {
ViewController *vc = [segue destinationViewController];
NSArray *homePlayersText = [HomePlayers componentsSeparatedByString:@" "];
vc.HomePlayersString = homePlayersText;
}
}
ViewController.h
@property (weak, nonatomic) IBOutlet UILabel *HomePlayersLabel;
@property (weak, nonatomic) NSArray *HomePlayersString;
ViewController.m
@synthesize HomePlayersString, HomePlayersLabel;
答案 0 :(得分:0)
您可能想要访问text
的{{1}}属性:
UITextField
其他评论:
NSArray *homePlayersText = [HomePlayers.text componentsSeparatedByString:@" "];
。homePlayers
属性应该声明强,而不是弱,
正如@rdelmar所指出的那样。否则HomePlayersString
将为vc.HomePlayersString
分配价值后不久。nil
语句。答案 1 :(得分:0)
如果要分离标签的组件,首先需要获取字符串值。作为你的榜样。
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"SaveData"]) {
ViewController *vc = [segue destinationViewController];
NSString * stringValue = self.HomePlayers.text;
NSArray *homePlayersText = [stringValue componentsSeparatedByString:@" "];
vc.HomePlayersString = homePlayersText;
}
}
另外,我建议你在开始时使用较低的上限编写类属性,因为这是苹果的建议之一。大写为“TestClass”的类,小写为“randomClass”的属性。