将单击的单元格文本发送到下一个视图

时间:2015-04-18 18:10:23

标签: objective-c sqlite uitableview segue

1.1,我有一个TableView,与sqlite链接,当我点击一个单元格时,程序进入prepareForSegue,然后获取tableView didSelectedRow中的值。

为此,在第一次访问secondView时,Label的值为nil,如果我回到上一个视图并选择其他单元格,我会得到第一个单元格值。

我在第二个视图中有一个UILabel,这个文本将是选中的单元格。

我不知道我怎么能在segue上说出tableView的文本值。

如果我的英语不好,可能有人可以帮助我,道歉。

感谢。

#import "farmacopeaViewController.h"

import“AddFarmacoViewController.h”//第二个View`

@implementation farmacopeaViewController

@synthesize copiaFarmaco,farmacos,descript;

- (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)didReceiveMemoryWarning {    //如果视图没有超视图,则释放视图。     [super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.

}

- (void)viewDidLoad {    [super viewDidLoad];

[self populateFarmacos];    [self populateDescrip]; }

-(void) populateFarmacos {   self.farmacos = [[NSMutableArray alloc] init];

FMDBDataAccess *db = [[FMDBDataAccess alloc] init];

self.farmacos = [db getFarmacos]; } -(void) populateDescrip {    self.descript = [[NSMutableArray alloc] init];

FMDBDataAccess *db = [[FMDBDataAccess alloc] init];

self.descript = [db getDescripcion]; }

- (void)viewDidUnload {     [super viewDidUnload];   }

- (void)viewWillAppear:(BOOL)animated {    [super viewWillAppear:animated]; }

- (void)viewDidAppear:(BOOL)animated {     [super viewDidAppear:animated]; }

- (void)viewWillDisappear:(BOOL)animated {    [super viewWillDisappear:animated]; }

- (void)viewDidDisappear:(BOOL)animated {    [super viewDidDisappear:animated]; }

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {     //返回YES表示支持的方向     return (interfaceOrientation == UIInterfaceOrientationPortrait); }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

// Return the number of sections.

return 1; }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

self.copiaFarmaco = [[[tableView cellForRowAtIndexPath:indexPath] textLabel]text]; NSLog(@"%@",copiaFarmaco); } -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if ([[segue identifier] isEqualToString:@"pasarDatos"]){

    AddFarmacoViewController *segundoView = [segue destinationViewController];

    segundoView.nombre = self.copiaFarmaco;
}

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return [self.farmacos count]; }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];   if (cell == nil){ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];     }  Farmaco *farmaco = [farmacos objectAtIndex:[indexPath row]];

[[cell textLabel] setText:[NSString stringWithFormat:@"%@",farmaco.farmacoId]]; return cell; }

@end

1 个答案:

答案 0 :(得分:2)

您应该从第一个ViewController创建segue到第二个ViewController(不是从Button到第二个ViewController)

didSelectRowAtIndexPath

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
         [self performSegueWithIdentifier:@"segueToLocation" sender:the object you want to pass];
    }

然后在这里获取对象

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString: @"segueToLocation"]) {    
         SecondViewController *dest = (SecondViewController *)[segue destinationViewController];
         //the sender is what you pass into the previous method
         dest.labelValue=sender
    }
}