2012-10-09 51 views
0

我是一個Objective-C的新手,並在特定的point.I必須傳遞一個UILabel值從tableviewcell滾動視圖中的標籤時,accessoryButtonTappedForRowWithIndexPath行爲發生。但值不通過..我donno在哪裏Iam出錯了?我寫這段代碼:將UItableviewcell的UILabel值傳遞給iPhone中的另一個視圖返回空值?

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]; 
} 

我可以看到標籤而不是價值它... Y就那麼如何一個UILabel值傳遞給另一種觀點?

回答

0

在你accessoryButtonTappedForRowWithIndexPath只是做一些修改如下,

添加

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

略高於

[self presentModalViewController:v2 animated:YES]; 

,也可以作爲provName在V2合成無需retain只是爲它分配..

編輯:要獲取單元格使用下面的

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

UITableViewCell也可以自定義單元格。

編輯:在CellForRow

變化更新您的cellForRow作爲

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *[email protected]"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; 

這應該可以正常工作。

相關問題