2013-06-18 141 views
3

我試圖將所選行的值傳遞給視圖控制器。日誌在新視圖控制器中返回空值。我的錯誤在哪裏?將選定行的值傳遞給視圖控制器

2013-06-18 17:28:59.394 App[1790:c07] ID is Here: 35023 

2013-06-18 17:28:59.397 App[1790:c07] Value in Next view (null) 

2013-06-18 17:28:59.398 App[1790:c07] Label is Here <UILabel: 0x9139810; 
frame = (14 140; 255 16); clipsToBounds = YES; opaque = NO; 
autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x915ab10>> 

TableViewController代碼:

NSString *localStringValue; 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    NSUInteger row = [indexPath row]; 
    NSString *value = [myArray objectAtIndex:row]; 

    DetailViewController *second=[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] ; 

    second.PassID=value; 

    [self presentModalViewController:second animated:YES]; 

    localStringValue = [tableView cellForRowAtIndexPath:indexPath].textLabel.text; 
    NSLog(@"ID is Here: %@", value); 
} 

下一頁查看控制器代碼:

.H

@interface DetailViewController : UIViewController 
{ 
    NSString *PassID; 
} 
@property (nonatomic, retain) NSString *PassID; 
@property (unsafe_unretained, nonatomic) IBOutlet UILabel *label; 

.M碼

- (void)viewDidLoad 
{ 

    label.text=PassID; 
    NSLog(@"Label is Here %@",label); 

    [super viewDidLoad]; 
} 
+0

嘗試打印self.label。 –

+1

讓iVar'label'和屬性'label'在任何地方都會引起混亂。刪除一個或另一個(從您的需要,我會說刪除iVar)。 Xcode會自動爲屬性創建一個iVar,通常爲_(iVar)。 – Putz1103

+0

嘗試通過先改變語句序列,然後賦值 [self presentModalViewController:second animated:YES]; second.label = value; – prasad

回答

1
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    NSUInteger row = [indexPath row]; 
    NSString *value = [myArray objectAtIndex:row]; 

    DetailViewController *second=[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] ; 

/// second.PassID=value; ///Remove the line here 


    [self presentModalViewController:second animated:YES]; 

    second.PassID=value; ///assign the value after presenting 


    localStringValue = [tableView cellForRowAtIndexPath:indexPath].textLabel.text; 
    NSLog(@"ID is Here: %@", value); 
} 
+0

in next view controller .m - (void)viewDidLoad { label.text = PassID; NSLog(@「Label is Here%@」,PassID); [super viewDidLoad];} Log:Label is Here(null) – Mhmt

0

嘗試NSLog(@"Label is Here %@", self.label);,看看是否有效。

您的.h文件中的這個0123arivar是沒有必要的,因爲您還聲明瞭一個具有相同名稱的屬性。

+0

我試過了。沒有解決 – Mhmt

0

在DetailViewController.h進行更改

@interface DetailViewController : UIViewController 
@property (nonatomic, retain) NSString *label; 

在DetailViewController.m

-(void)setLabelText:(NSString *)label{ 
    if(_label!=label) 
     _label = label; 
} 

和使用功能

[第二setLabelText:值]。

並打印與

的NSLog(@ 「標記是這裏%@」,self.label);

+0

它是一樣的。在新的視圖控制器中返回null。 – Mhmt

相關問題