2014-05-20 37 views
0

我有一个UITableView使用自定义表格视图单元格,单元格包含UITextLabelUIButton。我想在点击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; 
+0

检查'[self.allMyContact objectAtIndex:indexPath.row]'才通过它到你的下一个ViewController。 – Larme

+0

@Larme你是什么意思到“在通过前检查'[self.allMyContact objectAtIndex:indexPath.row]'”这个部分是否有问题? – rihe

+0

在将此值设置为'dataToNew.messageRecipient'之前进行记录。 – Larme

回答

1

我建议你使用这个小型图书馆来帮助你解决表格问题(我希望你熟悉Coc oaPods)

https://github.com/stefanomondino/SMQuickSegue

如果你有一个细胞SEGUE到下一个视图控制器,它应该有可能实现这一

#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]; 
} 
+0

感谢您的建议,但不幸的是我实际上不能使用CocoaPods,因为Parse与它有点儿差错。 – rihe

+0

你也可以在没有cocoapods的情况下使用它,只需克隆库并拖动项目中的两个文件(UIViewController + SMQuickSegue.h和UIViewController + SMQuickSegue.m)即可。 –

0

你的目标你的问题是,当你的Alloc局部变量messageRecipient那么你不会读你在头文件中声明的属性。

目的地方法改成这样,它应该工作(假设你正在使用您的属性自动合成)...

- (IBAction)dataTestButton:(id)sender { 

    NSLog (@"The string = %@", _messageRecipient); 

} 
相关问题