0

我是 Objective-C 的新手,并且在某个特定点被击中。我必须将 UILabel 值从 tableviewcell 传递到 Scrollview 中的标签,当附件ButtonTappedForRowWithIndexPath 动作发生时。但是该值没有传递..我不知道我哪里出错了? 我正在写这段代码:

    ViewController1.h:
UILabel *name1;
@property(nonatomic,retain)IBOutlet UILabel *name1;

    ViewController1.m:
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{

ViewController2 *v2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
v2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

[self presentModalViewController:v2 animated:YES];
 v2.provName=[name1 retain];   //name1 is the name of UILabel in TableView.
[v2 release];
}
    ViewController2.h
UILabel *providerName;
SString *provName;

    ViewController2.m:
- (void)viewDidLoad
{
providerName =[[UILabel alloc] init];
[providerName setFrame:CGRectMake(10,10,300,50) ];
providerName.textAlignment=UITextAlignmentLeft;
providerName.backgroundColor=[UIColor blackColor];

self.providerName.text=self.provName; 
 providerName.highlightedTextColor=[UIColor whiteColor];
[self.view addSubview:providerName];
}

我可以看到标签但看不到其中的值...是这样吗?如何将 UIlabel 值传递给另一个视图?

4

1 回答 1

0

在您accessoryButtonTappedForRowWithIndexPath只需进行如下更改,

添加

v2.provName=[name1 retain];   //name1 is the name of UILabel in TableView.

就在上面

[self presentModalViewController:v2 animated:YES];

并且作为 V2 中的 provName 是综合的,无需retain分配它..

编辑:要获取单元格,请使用以下命令

UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
v2.provName = cell.name1;

UITableViewCell也可以是您的自定义单元格。

编辑:变化CellForRow

更新你cellForRow

-(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]autorelease];
    }
    NSMutableDictionary *d = (NSMutableDictionary *) [arr objectAtIndex:indexPath.row];
    cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;

    UILabel* name1= [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 320, 10)];
    name1.font=[UIFont boldSystemFontOfSize:14];
    [name1 setTextAlignment:UITextAlignmentLeft];
    [name1 setText:[d valueForKey:@"Name"]];
    name1.tag = 111;
    [cell addSubview:name1];
    [name1 release];

    return cell;
}

不要将您的单元格和 name1 设为全局,仅在cellForRow

更新您didSelectRow的如下

UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel* name1 = (UILabel*)[cell viewWithTag:111];
v2.provName = name1.text;

这应该可以正常工作。

于 2012-10-09T17:55:22.087 回答