2016-04-29 67 views
0

我正在創建segue以便將數據從tableview控制器傳遞到詳細視圖控制器as shown in the picture。我的表單元格 有一個UIImage和兩個UILabel,我想在其中詳細顯示三個視圖控制器。但是,當我運行該程序,它看起來像this空。從tableview控制器傳遞數據到視圖控制器,但它顯示爲空

這裏是tableviewcontroller.m

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    NSLog(@"prepareForSegue:%@", segue.identifier); 



    if([segue.identifier isEqualToString:@"TeamMembersSegue"]){ 

     NSIndexPath*indexPath = (NSIndexPath*)sender; 

     ViewController2*vc = ((UINavigationController*)segue.destinationViewController).topViewController; 

     vc.lawfirmname = [tableData objectAtIndex:indexPath.row]; 
     vc.detail= [detail objectAtIndex:indexPath.row]; 
     vc.lawyerimage = [thumbnails objectAtIndex:indexPath.row]; 
} 
} 

的SEGUE和detailviewcontroller.h的代碼(視圖控制器2)

#import <UIKit/UIKit.h> 


@interface ViewController2 : UIViewController 

@property (nonatomic, strong) NSString *lawfirmname; 
@property (strong, nonatomic) IBOutlet UILabel *lawfirm; 

@property (nonatomic, strong) NSString *detail; 
@property (weak, nonatomic) IBOutlet UITextView *detailtextview; 

@property (nonatomic, strong) UIImage*lawyerimage; 
@property (weak, nonatomic) IBOutlet UIImageView *image; 

@end 

和detailviewcontroller.m(視圖控制器2)

#import "ViewController2.h" 

    @interface ViewController2() 

    @end 

    @implementation ViewController2 


    @synthesize lawfirmname; 
    @synthesize lawfirm; 
    @synthesize detail; 
    @synthesize detailtextview; 
    @synthesize lawyerimage; 
    @synthesize image; 

    - (void)viewDidLoad { 
     [super viewDidLoad]; 


     // Do any additional setup after loading the view. 

     self.lawfirm.text = self.lawfirmname; 
     self.detailtextview.text = self.detail; 
     self.image.image = self.lawyerimage; 
    } 

    - (void)didReceiveMemoryWarning { 
     [super didReceiveMemoryWarning]; 
     // Dispose of any resources that can be recreated. 
    } 



    /* 
    #pragma mark - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
     // Get the new view controller using [segue destinationViewController]. 
     // Pass the selected object to the new view controller. 
    } 
    */ 

    @end 

如果有人有任何解決方法,我將不勝感激。如果你不明白這個問題,請告訴我。

+0

你是否檢查vc.lawfirmname和其他屬性是否在yuor prepareForSegue中設置? –

+0

您確定segue的目的地是導航控制器嗎? – vadian

+0

您可能想要確保您在prepareForSegue中使用選定的行 –

回答

1

使用didSelectRowAtIndexPath方法從你的tableview控制器的數據傳遞到詳細信息視圖控制器。例如: -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    self.selectedRowModel = [self.someArray objectAtIndex:indexPath.row]; 
    [self performSegueWithIdentifier:@"TeamMembersSegue" sender:self]; 
} 

然後覆蓋prepareForSegue方法

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if([segue.identifier isEqualToString:@"TeamMembersSegue"]){ 
     ViewController2 *vc = segue.destinationViewController; 
     vc.selectedModel = self.selectedRowModel; 
    } 
} 

注: -創建一個共用行模型來表示你的個人行項目。模型將爲UITableView中的每一行存儲您的圖像URL和圖像描述。

+0

謝謝,但你知道創建普通行模型的方法嗎? –

+0

是的,您可以創建一個可以用作模型的NSObject類。 – Jitendra

0

眼下SEGUE傢伙後,我改變

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
     [self performSegueWithIdentifier:@"showlawfirmdetail" sender:indexPath]; 
    } 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    NSLog(@"prepareForSegue:%@", segue.identifier); 



    if([segue.identifier isEqualToString:@"showlawfirmdetail"]){ 

     NSIndexPath*indexPath = (NSIndexPath*)sender; 

     ViewController2*vc = (ViewController2*)segue.destinationViewController; 

     vc.lawfirmname = [tableData objectAtIndex:indexPath.row]; 
     vc.detail= [detail objectAtIndex:indexPath.row]; 
     vc.lawyerimage = [thumbnails objectAtIndex:indexPath.row]; 
} 
} 

它的工作原理部分。剩下的問題是我不能在視圖控制器2中添加圖像。它崩潰。但是,如果我刪除代碼self.image.image = self.lawyerimage;應用程序運行平穩。

+0

是lawyerimage UIImage或UIImageView?因爲如果它是一個UIImageView,你應該設置:self.image.image = self.lawyerimage.image; –

+0

我已按照你的指示,但仍然存在一個綠色的bug,稱爲線程1:斷點1.1 –

0

@Herman再來一次。我創建了一個項目並摒棄了所有的代碼,只是爲了看看我是否得到了我沒有的東西。所以我沒有弄清楚它出了什麼問題。

有一件事我認爲是可能的,如果你由於某種原因失去了IBOutlet連接或者它是重複的。檢查出來,就像在圖像中一樣。見右上角只有一個出口爲參照:

enter image description here

這裏是我的「TableViewController.h」的代碼,有一件事我做它不同的設置編程的NSString的和的UIImage:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

    if([segue.identifier isEqualToString:@"TeamMembersSegue"]){ 
     ViewController2 *vc = segue.destinationViewController; 
     vc.lawfirmname = @"Title"; 
     vc.detail = @"Detail here"; 
     vc.lawyerimage = [UIImage imageNamed:@"print"]; 
    } 

} 

我的「ViewController2。H」是一樣的你:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    self.lawfirm.text = self.lawfirmname; 
    self.detailtextview.text = self.detail; 
    self.image.image = self.lawyerimage; 
} 

看看,它的正常工作:

enter image description here

+0

是啊,它是非常奇怪,它不斷爲圖像查錯,但我會再試一次 –

+0

問題解決只是想知道如何傳遞圖像的數據從plist –

+0

每一件事情都可以現在是一個巨大的緩解。 我只是把我的代碼: vc.lawyerimage = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]]; 而不是 vc.lawyerimage = [縮略圖objectAtIndex:indexPath.row]; –

0

我已經得到了解決: 更換

vc.lawyerimage = [thumbnails objectAtIndex:indexPath.row]; 

通過

vc.lawyerimage = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]]; 
0

我建議你在你的第二個視圖控制器中聲明三個不同的字符串,然後將這個單元格的數據從你的prepareForSegue方法中傳遞給這些字符串。在這之後,用viewDidLoadviewWillAppear這三個字符串填充IBOutlets。這解決了我的問題類似於此。希望這是有幫助的。

相關問題