在自定义表格视图单元格按钮操作中导航到另一个 Viewcontroller

Posted

技术标签:

【中文标题】在自定义表格视图单元格按钮操作中导航到另一个 Viewcontroller【英文标题】:Navigate to another Viewcontroller in Custom Table View Cell Button Action 【发布时间】:2013-10-02 10:33:04 【问题描述】:

我有一个 TableViewCell,其中有一个按钮 Zoom Button。当我单击缩放按钮时,视图将导航到 PlayerViewController。

(https://www.dropbox.com/s/ikmj4vdfc407ldy/customcell.png)

这是我的播放按钮代码。

-(IBAction)downloadAction:(id)sender 

            BookPlayerViewController *bookPlayerViewController=[BookPlayerViewController alloc];
            [self.navigationController pushViewController:bookPlayerViewController animated:YES];

我该如何解决这个问题?我正在使用 Nib 而不是情节提要来实施该项目。

【问题讨论】:

不应该是BookPlayerViewController *bookPlayerViewController=[BookPlayerViewController alloc]init]; 我试过 BookPlayerViewController *bookPlayerViewController=[BookPlayerViewController alloc]init]; , 但它没有工作 你应该试试 BookPlayerViewController *vcBookPlayerViewController = [[BookPlayerViewController alloc] initWithNibName:@"BookPlayerViewController" bundle:nil]; [self.navigationController pushViewController:vcBookPlayerViewController 动画:YES]; 【参考方案1】:

您没有初始化笔尖名称。您需要使用 NibName 初始化 BookPlayerViewController 才能显示它,

使用 IB

BookPlayerViewController *vcBookPlayerViewController = [[BookPlayerViewController alloc] 
initWithNibName:@"BookPlayerViewController" bundle:nil]; 

使用故事板

BookPlayerViewController *vcBookPlayerViewController = [storyboard
instantiateViewControllerWithIdentifier:@"BookPlayerViewController"];

【讨论】:

【参考方案2】:
I  created an outlet property in the tableviewcell class
@property (retain, nonatomic) IBOutlet UIButton *zoomButton;

然后我在tableviewCellForRowAtIndex方法的超类中添加目标

[cell.zoomButton addTarget:self action:@selector(navigateAction:) forControlEvents:UIControlEventTouchUpInside];

然后我使用 NavigateAction 的 IBAction 我编写了从一个 ViewController 导航到另一个 ViewController 所需的那些操作。

-(IBAction)navigateAction:(id)sender

*UIButton *btn = (UIButton *)sender;
int indexrow = btn.tag;
NSLog(@"Selected row is: %d",indexrow);

currentBook = [[bookListParser bookListArray] objectAtIndex:indexrow];

KitapDetayViewController *kitapDetayViewController;
if(IS_IPHONE_5)

    kitapDetayViewController = [[KitapDetayViewController alloc] initWithNibName:@"KitapDetayViewController" bundle:Nil];


else

    kitapDetayViewController = [[KitapDetayViewController alloc] initWithNibName:@"KitapDetayViewController_iPhone4" bundle:Nil];

kitapDetayViewController.bookId=currentBook.bookId;
kitapDetayViewController.detailImageUrl = currentBook.detailImageUrl;
kitapDetayViewController.bookAuthor = currentBook.bookAuthor;
kitapDetayViewController.bookName = currentBook.bookName;
kitapDetayViewController.bookDescription = currentBook.bookDescription;
kitapDetayViewController.bookNarrator=currentBook.bookNarrator;
kitapDetayViewController.bookOrderHistory=currentBook.bookOrderDate;
int byte=[currentBook.bookSizeAtByte intValue];
int mb=byte/(1024*1024);
NSString *mbString = [NSString stringWithFormat:@"%d", mb];
kitapDetayViewController.bookSize=mbString;
kitapDetayViewController.bookOrderPrice=currentBook.priceAsText;
int second=[currentBook.bookDuration intValue];
int minute=(second/60)%60;
int hour=(second/3600)%60;
int sec = second % 60;

NSString *formattedTime = [NSString stringWithFormat:@"%d:%d:%d", hour, minute, sec];
kitapDetayViewController.bookDuration=formattedTime;
kitapDetayViewController.chapterNameListArray=self.chapterNameListArray;
// [[bookListParser bookListArray ]release];
[self.navigationController pushViewController:kitapDetayViewController animated:YES];*

这样我就解决了这个问题。

【讨论】:

【参考方案3】:

如果您正在加载 nextview 的 nib,请尝试这种方式

BookPlayerViewController *vcBookPlayerViewController = [[BookPlayerViewController alloc] initWithNibName:@"BookPlayerViewController" bundle:nil]; 
[self.navigationController pushViewController:vcBookPlayerViewController animated:YES];

【讨论】:

【参考方案4】:

你错过了笔尖,看看这段代码..

-(IBAction)downloadAction:(id)sender 

            BookPlayerViewController *bookPlayerViewController=[BookPlayerViewController alloc]initWithNibName:@"BookPlayerViewController" bundle:nil];;
            [self.navigationController pushViewController:bookPlayerViewController animated:YES];

【讨论】:

以上是关于在自定义表格视图单元格按钮操作中导航到另一个 Viewcontroller的主要内容,如果未能解决你的问题,请参考以下文章

从自定义表格视图单元格获取数据到另一个 VC?

如何停止在自定义表格视图单元格中交换数据?

以swift语言在自定义表格视图单元格中更新进度视图下载

无法在自定义表格视图单元格中使 ImageView 成为一个圆圈

在自定义表格视图单元格问题中水平/垂直约束

如何在自定义表格视图单元格中保存文本字段