簡體   English   中英

如何在表視圖中選擇后將數據傳遞到詳細視圖?

[英]How to pass data to detail view after being selected in a table view?

我有一個UITableViewController,其中包含從名為userList的NSMutableArray填充的數據。 選擇特定用戶后,它將轉到詳細視圖,並更新NavBar中的標題,但它不會傳遞數據以更新UIView中的UILabel。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[tableView deselectRowAtIndexPath:indexPath animated:NO];

//Attempt at a singleton to pass the data
DetailView *details = [[DetailView alloc] init];
details.userNameLbl = [userList objectAtIndex:indexPath.row];


DetailView *detailVC = [[DetailView alloc] initWithNibName:nil bundle:nil];

//This line doesn't pass the data
detailVC.userNameLbl.text = [userList objectAtIndex:indexPath.row];

//This line does update the title in the NavBar
detailVC.navigationItem.title = [userList objectAtIndex:indexPath.row];

[self.navigationController pushViewController:detailVC animated:YES];

[details release];
[detailVC release];

}

我應該嘗試將數據從表視圖推送到詳細視圖,還是讓詳細視圖嘗試從表視圖中提取數據?

DetailView.h實際上是在IB中連接的以下行。

IBOutlet UILabel *userNameLbl

在從nib文件加載視圖之前,不會實例化userNamelbl 這不會在初始化時立即發生,但會在調用viewDidLoad發生。

因此,您應該在DetailView中聲明一個屬性來存儲您的標題,然后將該值分配給viewDidLoad方法中的userNamelbl.text

例如,在你的表viewController中:

DetailView *detailVC = [[DetailView alloc] initWithNibName:nil bundle:nil];
detailVC.userName = [userList objectAtIndex: indexPath.row];

並在您的詳細視圖中控制:

- (void) viewDidLoad
{
   [super viewDidLoad];
   self.userNameLbl.text = self.userName;
}

viewController的navigationItem屬性是在初始化viewController時創建的,因此您可以立即分配給navigationItem.title


SWIFT代碼

let detailVC = DetailView(nibName: nil, bundle: nil)
detailVC.userName = userList.objectAtIndex(indexPath.row) as? NSString

class DetailView: UIViewController {

    @IBOutlet var userNameLbl: UILabel
    var userName:NSString?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.userNameLbl.text = self.userName
    }    
} 

你在混合UIViewsUIViewControllers嗎? 你應該有一個繼承自UIViewController或某個sublcass(如UITableViewController )的DetailViewController類; 它將具有DetailViewController.mDetailViewController.h文件來聲明和定義您的類。 它應該有一個相應的nib,它定義了UIViewController加載的UIView ; 它將有一個DetailView.xib文件。

您無法直接將值分配給UILabel ,因為在您需要分配用戶名值時尚未加載UIView

為了做你想做的事,你應該聲明一個公共屬性( userName ),將值從主控制器“推”到詳細視圖控制器上。 加載詳細信息視圖后,它可以將屬性中的值分配給標簽和導航欄。

在主視圖控制器( UITableViewController )中:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

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

    detailVC.userName = [userList objectAtIndex:indexPath.row];

    [self.navigationController pushViewController:detailVC animated:YES];

    [detailVC release];
}

在您的詳細視圖控制器中:

DetailViewController.h

@property (retain) NSString* userName;
@property (nonatomic, retain) IBOutlet UILabel *userNameLbl;

DetailViewController.m

@synthesize userName;
@synthesize userNameLbl;

-(void) viewDidLoad {
    [super viewDidLoad];
    self.userNameLbl.text = self.userName;
    self.navigationItem.title = self.userName;
}

大概你的DetailView需要你所選行的數據才能運行?

如果是這樣,我會說“正確”的方法是創建一個傳遞所需數據的自定義init方法。 例如:

[[DetailView alloc] initWithTitle:(NSString *)title text:(NSString *)text]

您的方法沒有任何本質上的錯誤,但是傳遞視圖控制器在創建時所需的數據在架構上更好(在我看來,我確信有人會不同意!)。 例如,考慮一個表視圖控制器 - 您在init方法中傳遞表視圖樣式,稍后不再設置它。 對於UIImageView - 您有一個initWithImage方法,可以讓您在創建時設置圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM