我有UITableView
使用自定义表格视图单元格,该单元格包含UITextLabel
和UIButton
。我想在UIButton
点击时将textlabel的值传递给另一个视图。我正在使用名为goToNewView
的segue传递数据。在目标视图控制器中,我有一个按钮,当点按NSLog
时,该按钮应显示带有传递值的dataTestButton
。实际上,当我点击dataTestButton
时,我没有收到任何错误,但是没有显示所需的数据(来自mySimpleContactUsernameLabel
)。仅显示The string =
。
我确定我的prepareForSegue
方法中的某些内容不正确,但我无法弄清楚如何告诉该方法,我想从按下该按钮的同一标签传递数据。如果有人能告诉我正确的方法,我真的很感激。
以下是我想传递数据的视图控制器的代码:
- (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;
答案 0 :(得分:1)
我建议你使用这个小库来帮助你解决表格问题(我希望你熟悉CocoaPods)
https://github.com/stefanomondino/SMQuickSegue
如果您有一个单元格转到下一个视图控制器,应该可以实现这个目标
#import <UIViewController+SMQuickSegue.h>
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableCell *cell = [tableViewTwo dequeueReusableCellWithIdentifier:@"devCell"];
PFUser *user = [self.allMyContact objectAtIndex:indexPath.row];
cell.mySimpleContactUsernameLabel.text = user.username;
[cell setSegueParameters:@{@"messageRecipient":user.name}]; //will set messageRecipient on the destination view controller with the name property of user.
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
[super prepareForSegue:segue sender:sender];
[self quickPrepareForSegue:segue sender:sender];
}
答案 1 :(得分:0)
您的问题是当您分配本地变量messageRecipient
时,您没有读取您在头文件中声明的属性。
将您的目标方法更改为此方法,它应该有效(假设您正在为您的属性使用自动合成)...
- (IBAction)dataTestButton:(id)sender {
NSLog (@"The string = %@", _messageRecipient);
}