簡體   English   中英

執行由UITableViewCell調用的Segue

[英]Perform a Segue Called by a UITableViewCell

我在自定義表格視圖單元格中有一些UIButtons,當我按下時,我希望它們在視圖控制器中調用segues,該視圖控制器是單元格所在的表視圖的主機。我現在正在做的是聲明這樣的動作: - (空隙)的動作;

在表視圖所在的類中。然后我從單元格調用該操作,如下所示:

ViewController *viewController = [[ViewController alloc] init];
[viewController action];

然而,當調用該動作時,它表示視圖控制器沒有這樣的segue,我知道這是不正確的。 從視圖控制器本身調用“動作”可以完美地工作,而不是從單元格。

此外,這是執行segue的代碼:

-(void)action {
     [self performSegueWithIdentifier:@"action" sender:self];
}

我怎樣才能做到這一點?

任何建議表示贊賞。

更新

我只是試圖像這樣在單元格上設置一個委托:

Cell的頭文件:@class PostCellView;

@protocol PostCellViewDelegate
-(void)action;
@end

@interface PostCellView : UITableViewCell <UIAlertViewDelegate, UIGestureRecognizerDelegate>

@property (weak, nonatomic) id <PostCellViewDelegate> delegate;

在單元格的主文件中,我調用這樣的動作:[self.delegate action];

並在承載表視圖的視圖的標題中我導入單元格如下:#import“PostCellView.h”

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, PostCellViewDelegate>

然后在該視圖的主文件中我有單元格操作:

-(void)action {

    [self performSegueWithIdentifier:@"action" sender:self];

}

但我檢查過,行動從未執行過。 當我從單元格調用動作時我記錄了並且我在動作本身上放置了一個NSLog,並且從單元格調用它的日志工作但不是動作。

我認為你的問題是這個ViewController *viewController = [[ViewController alloc] init]; [viewController action]; ViewController *viewController = [[ViewController alloc] init]; [viewController action]; 您正在啟動一個新的ViewController,而不是獲取該單元所在的當前ViewController。

也許您可以在單元格上設置委托,以便單元格具有對viewController的引用

創建按鈕並以編程方式調用其處理程序的簡單方法是

UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(100, 100, 80, 40)];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button]; //self.view is the view on which you want to add button

並且它的處理程序應該在您的實現文件中定義如下:

-(void)buttonClick:(id)sender{
    NSLog(@"button clicked");
    // your code on click of button 
}

您可以查看iOS文檔

希望這能幫助您找出問題所在。 :)

暫無
暫無

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

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