我正在创建segue以便将数据从tableview控制器传递到详细信息视图控制器as shown in the picture。我的表格单元
 有一个UIImage和两个UILabel,我想在详细视图控制器中显示其中三个。但是,当我运行该程序时,它像this一样显示为null。

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


和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


如果有人有任何解决方案,我将不胜感激。如果您不明白这个问题,请通知我。

最佳答案

使用didSelectRowAtIndexPath将数据从表视图控制器传递到详细信息视图控制器。例:-


- (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和图像描述。

关于ios - 将数据从tableview Controller 传递到 View Controller ,但它显示为null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36943541/

10-10 04:55