繁体   English   中英

快速调用协议方法

[英]protocol method not called in swift

我已经在目标c文件中快速创建了协议,但是协议方法没有在这里调用

@Swift文件

@objc protocol clickBookmarksProtocolDelegate
{
 func openbook(bookmark : String)
}
var delegate : clickBookmarksProtocolDelegate?

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  a = data[indexPath.row][main]
  println(a)
  if (a == "Bookmarks")
  {
     self.delegate?.openbook(data[indexPath.row][url])
     dismissViewControllerAnimated(false, completion: nil)
  }

@obj C .m文件

#import "Video_Downloader-Swift.h"

@interface BrowserViewController() <clickBookmarksProtocolDelegate>

@end

- (void)openbook:(NSString * __nonnull)bookmark
{
 [self loadAddress:bookmark];
 _addressBar.text=bookmark;
}

方法不调用

好吧,您不应该为此使用委托。 代表的使用类似于某种回调。 例如,假设您有一个控制器,它将显示另一个控制器:

FirstViewController -> SecondViewController

在您的SecondViewController想要调用FirstViewController的情况下,将使用代理。

在您的情况下,您只想显示SecondViewController,为此您有一些选择。

如果您拥有导航视图控制器,最简单的方法就是在didSelectCell方法中推送新的BrowserViewController:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.navigationController?.pushViewController(BrowserViewController(), animated: true)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM