我有一个UITableView
使用自定义表格视图单元格,单元格包含UITextLabel
和UIButton
。我想在点击UIButton
时将文本标签的值传递给另一个视图。我正在使用名为goToNewView
的segue来传递数据。在目标视图控制器中,我有一个按钮,当dataTestButton
点击时,该按钮应显示带有传递值的NSLog
。其实我没有错误,当我点击dataTestButton
,但所需的数据(从mySimpleContactUsernameLabel
)不会出现。只有The string =
出现了。如何正确传递UITextlabel的值到另一个视图
我敢肯定,在我的prepareForSegue
方法中的东西是不正确的,但我不知道如何告诉该方法,我想通过按钮按下相同的标签的数据。所以如果有人能以正确的方式向我展示,我将不胜感激。
这里的视图控制器从其中i要传递的数据的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.simpleContact = [[PFUser currentUser] objectForKey:@"simpleContact"];
}
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
PFQuery *queryMyContacts = [self.simpleContact query];
[queryMyContacts orderByAscending:@"username"];
[queryMyContacts findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error %@ %@", error, [error userInfo]);
}
else {
self.allMyContact = objects;
[tableViewTwo reloadData];
}
}];
}
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.allMyContact count];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableCell *cell = [tableViewTwo dequeueReusableCellWithIdentifier:@"devCell"];
PFUser *user = [self.allMyContact objectAtIndex:indexPath.row];
cell.mySimpleContactUsernameLabel.text = user.username;
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"goToNewView"]) {
if ([[segue destinationViewController] isKindOfClass:[NewDestinationViewController class]]) {
NSIndexPath *indexPath = [tableViewTwo indexPathForSelectedRow];
NewDestinationViewController *dataToNew = [segue destinationViewController];
dataToNew.messageRecipient = [self.allMyContact objectAtIndex:indexPath.row];
}
}
}
这里的目的地视图控制器的.m文件:
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)dataTestButton:(id)sender {
NSString *messageRecipient = [[NSString alloc] init];
NSLog (@"The string = %@", messageRecipient);
}
h文件:
@property (nonatomic, strong) NSString *messageRecipient;
- (IBAction)dataTestButton:(id)sender;
检查'[self.allMyContact objectAtIndex:indexPath.row]'才通过它到你的下一个ViewController。 – Larme
@Larme你是什么意思到“在通过前检查'[self.allMyContact objectAtIndex:indexPath.row]'”这个部分是否有问题? – rihe
在将此值设置为'dataToNew.messageRecipient'之前进行记录。 – Larme