GVKun编程网logo

Xcode 7 Swift 2无法实例化通用UITableViewController的UIViewController子类

11

对于Xcode7Swift2无法实例化通用UITableViewController的UIViewController子类感兴趣的读者,本文将会是一篇不错的选择,并为您提供关于ios–Swift–如何

对于Xcode 7 Swift 2无法实例化通用UITableViewController的UIViewController子类感兴趣的读者,本文将会是一篇不错的选择,并为您提供关于ios – Swift – 如何从ViewController中按下UItableViewCell中的动作按钮?、ios – Swift:类型’ViewController’不符合协议’UIPageViewControllerDataSource’、ios – UINavigationController里面的UITabBarController里面的UITableViewController、ios – UITableViewCell内的UIPageViewController的有用信息。

本文目录一览:

Xcode 7 Swift 2无法实例化通用UITableViewController的UIViewController子类

Xcode 7 Swift 2无法实例化通用UITableViewController的UIViewController子类

我有一个通用类:

class PaginatedTableViewController  <GenericElement, Source: PaginationDataSource      where Source.PaginatedGenericElement == GenericElement>:  UITableViewController

我尝试从情节提要中实例化的另一个:

class CandidatesTableViewController:    PaginatedTableViewController<Match, MatchPaginationDataSource>

CandidatesTableViewController在情节提要中的“自定义类”下拉菜单中找不到。如果我强行使用它,然后将我的控制器转换为代码,则应用程序在运行时崩溃,抱怨我的控制器(应该是CandidatesTableViewController实例)实际上是一个UITableViewController实例。

接口生成器文件中的未知类_TtC21MyProjectName29CandidatesTableViewController。无法将类型’UITableViewController’(0x1040917f8)的值强制转换为’MyProjectName.CandidatesTableViewController’(0x1013a9890)。

在我的项目中,此控制器嵌入到另一个控制器中,这就是我强制转换的原因:

tableViewController = (segue.destinationViewController as! CandidatesTableViewController)

有谁知道如何解决这个问题?

答案1

小编典典

不幸的是,通用Swift类对于Objective-C代码不可见,并且在Interface
Builder中(在情节提要和Xibs中)也不受支持。我发现这两点密切相关。

作为一种解决方案,我建议您使用聚合:不要使您的视图控制器具有通用性,而是将某些逻辑提取到另一个(泛型)类中并在视图控制器内部使用它。

ios – Swift – 如何从ViewController中按下UItableViewCell中的动作按钮?

ios – Swift – 如何从ViewController中按下UItableViewCell中的动作按钮?

参见英文答案 > Issue Detecting Button cellForRowAt                                    3个
>             swift: how to get the indexpath.row when a button in a cell is tapped?                                    14个
我在UITableViewCell中有一个操作按钮,我想检测按钮的按下时间以及ViewController中按下的单元格编号,以便在ViewController.swift中创建音频播放列表.

我已经陷入这个问题一段时间了,我真的很赞赏你的建议.这是代码.

ViewController.swift

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self

        tableView.register(UINib(nibName: "Cell",bundle: nil),forCellReuseIdentifier: "cell")

    }

    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! Cell
        return cell

    }


}

Cell.swift

import UIKit

class Cell: UITableViewCell {

    @IBOutlet weak var button: UIButton!

    @IBAction func buttonpressed(_ sender: Any) {

        ***[Code to send the pressed cell's number to ViewController]***

    }

}

解决方法

你可以选择一个老式的代表模式.这样做的好处是不会将视图控制器与单元格耦合.不要忘记让你的代表弱,以避免保留周期.

您可以从表视图中找到单元索引路径. (我假设按单元格编号表示索引路径)

protocol CellDelegate: class {
    func didTap(_ cell: Cell)
}

class Cell: UITableViewCell {

    weak var delegate: CellDelegate?
    @IBAction func buttonpressed(_ sender: Any) {
        delegate?.didTap(self)
    }
}

class ViewController: UIViewController,CellDelegate {

    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = ...
        cell.delegate = self
        return cell
    }

    func didTap(_ cell: Cell) {
        let indexPath = self.tableView.indexPath(for: cell)
        // do something with the index path
    }
}

ios – Swift:类型’ViewController’不符合协议’UIPageViewControllerDataSource’

ios – Swift:类型’ViewController’不符合协议’UIPageViewControllerDataSource’

我正在使用 Xcode 6 GM.我正在尝试实现这个 page view controller tutorial但是在Swift中而不是Objective-C但它没有按预期工作.

我实际上设法找到了其他人正在做同样的git repo,但是在克隆他们的项目并在Xcode中打开它之后,我遇到了同样的错误.在实现UIPageViewControllerDataSource协议时,除了协议一致性问题之外,我已经设法解决了大部分问题.

说实话,我不完全明白其用法?而且!在斯威夫特,如果这是导致我的问题.删除!从协议的方法实现中的变量引起其他错误.

有人可以帮忙吗?

class ViewController: UIViewController,UIPageViewControllerDataSource {

var pageViewController : UIPageViewController?
var pageTitles = ["Over 200 Tips and Tricks","discover Hidden Features","Bookmark Favorite Tip","FreeRegular Update"]
var pageImages = ["page1.png","page2.png","page3.png","page4.png"]
var currentIndex = 0

@IBAction func startWalkthrough(sender: UIButton) {
    var startingViewController : PageContentViewController = self.viewControllerAtIndex(0)!
    var viewControllers : NSArray = [startingViewController]
    self.pageViewController!.setViewControllers(viewControllers,direction: .Forward,animated: false,completion: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()

    //Create page view controller
    self.pageViewController = UIPageViewController(transitionStyle: .Scroll,navigationorientation: .Horizontal,options: nil)
    self.pageViewController!.dataSource = self

    let startingViewController : PageContentViewController = self.viewControllerAtIndex(0)!
    let viewControllers: NSArray = [startingViewController]
    self.pageViewController!.setViewControllers(viewControllers,completion: nil)

    // Change the size of page view controller
    self.pageViewController!.view.frame = CGRectMake(0,self.view.frame.size.width,self.view.frame.size.height - 30);

    self.addChildViewController(self.pageViewController!)
    self.view.addSubview(self.pageViewController!.view)
    self.pageViewController!.didMovetoParentViewController(self)

}

func pageViewController(pageViewController: UIPageViewController!,viewControllerBeforeViewController viewController: UIViewController!) -> UIViewController! {

        var index = (viewController as PageContentViewController).pageIndex

        if index == 0 || index == NSNotFound {
            return nil
        }

        index!--

        println("Decreasing Index: \(index)")

        return self.viewControllerAtIndex(index!)
}

func pageViewController(pageViewController: UIPageViewController!,viewControllerAfterViewController viewController: UIViewController!) -> UIViewController! {

        var index = (viewController as PageContentViewController).pageIndex

        if index == NSNotFound {
            return nil
        }

        index!++

        println("Increasing Index: \(index)")

        if index == self.pageTitles.count {
            return nil;
        }
        return self.viewControllerAtIndex(index!);
}

func viewControllerAtIndex(index : Int) -> PageContentViewController? {

    if self.pageTitles.count == 0 || index >= self.pageTitles.count {
        return nil;
    }

    // Create a new view controller and pass suitable data.
    let pageContentViewController = self.storyboard!.instantiateViewControllerWithIdentifier("PageContentViewController") as PageContentViewController
    pageContentViewController.imageFile = self.pageImages[index]
    pageContentViewController.titleText = self.pageTitles[index]
    pageContentViewController.pageIndex = index

    return pageContentViewController;
}

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    return self.pageTitles.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    return 0
}
}

解决方法

这是因为UIPageViewControllerDataSource协议具有更新的方法签名 – 您正在使用:
func pageViewController(pageViewController: UIPageViewController!,viewControllerBeforeViewController viewController: UIViewController!) -> UIViewController!

func pageViewController(pageViewController: UIPageViewController!,viewControllerAfterViewController viewController: UIViewController!) -> UIViewController!

但现在它们是:

func pageViewController(pageViewController: UIPageViewController,viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?

func pageViewController(pageViewController: UIPageViewController,viewControllerAfterViewController viewController: UIViewController) -> UIViewController?

当您对不符合协议有疑问时,命令单击协议名称将带您进入协议声明,您可以在其中查看是否正确实现其接口

ios – UINavigationController里面的UITabBarController里面的UITableViewController

ios – UINavigationController里面的UITabBarController里面的UITableViewController

我的问题

我在UINavigationController中使用UITabBarController. UITabBarController中有三个tableviews.如图所示,第一个表视图正确显示,而其他两个tableviews部分隐藏在导航栏后面.我怎样才能解决这个问题?

我的等级:

> Root:UINavigationController

> UITabBarController

> UITableViewController(table1,table2,table3)

这是我的代码:

AppDelegate.m

#import "AppDelegate.h"
#import "TableViewController.h"
@interface AppDelegate()
@property UINavigationController* nav;
@end

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    TableViewController* table1 = [[TableViewController alloc]init];
    TableViewController* table2 = [[TableViewController alloc]init];
    TableViewController* table3 = [[TableViewController alloc]init];
    table1.title = @"table1";
    table2.title = @"table2";
    table3.title = @"table3";
    UITabBarController* t = [[UITabBarController alloc] init];
    [t setViewControllers:@[table1,table3]];
    self.nav = [[UINavigationController alloc] initWithRootViewController:t];
    [self.window setRootViewController:self.nav];
    [self.window makeKeyAndVisible];
    return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application{}
- (void)applicationDidEnterBackground:(UIApplication *)application{}
- (void)applicationWillEnterForeground:(UIApplication *)application{}
- (void)applicationDidBecomeActive:(UIApplication *)application{}
- (void)applicationWillTerminate:(UIApplication *)application{}
@end

TableViewController.m

#import "TableViewController.h"
@implementation TableViewController
- (id)initWithStyle:(UITableViewStyle)style{
    self = [super initWithStyle:style];
    if (self) {}
    return self;
}
- (void)viewDidLoad{
    [super viewDidLoad];
    [self.tabBarController.view layoutSubviews];
}
- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* c = [[UITableViewCell alloc] init];
    [c.textLabel setText:[Nsstring stringWithFormat:@"%d",indexPath.row]];
    return c;
}
@end

解决方法

通常,层次结构是

UITabBarController
    - UINavigationController
        - UITableViewController

你为什么要把导航控制器放在最上面?尝试使用充满导航控制器的标签栏重新组织.

ios – UITableViewCell内的UIPageViewController

ios – UITableViewCell内的UIPageViewController

嘿,我想问一下如何在UITableViewCell中实现UIPageViewController.

我一直在阅读,但到目前为止似乎没有任何人尝试.

我会很感激一些提示,不需要一个完整的答案..

谢谢!.

解决方法

目前还不清楚你到底想要做什么,但让我看看能否解释一下.您希望在表视图单元格中具有页面视图控制器视图.

您需要为每个单元格创建一个页面视图控制器,将其视图调整为单元格内容视图的大小,并将视图添加为单元格的子视图.然后,您必须实现页面视图控制器的数据源并在单元格内委托方法.

就像是:

self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationorientation:UIPageViewControllerNavigationorientationHorizontal options:nil];
[self.pageViewController setDataSouce:self];
[self.pageViewController setDelegate:self];
[self.pageViewController.view setFrame:self.contentView.bounds];
self.contentView addSubview:self.pageViewController.view];

然而,这是一种奇特的设计,看起来过于复杂.

我们今天的关于Xcode 7 Swift 2无法实例化通用UITableViewController的UIViewController子类的分享就到这里,谢谢您的阅读,如果想了解更多关于ios – Swift – 如何从ViewController中按下UItableViewCell中的动作按钮?、ios – Swift:类型’ViewController’不符合协议’UIPageViewControllerDataSource’、ios – UINavigationController里面的UITabBarController里面的UITableViewController、ios – UITableViewCell内的UIPageViewController的相关信息,可以在本站进行搜索。

本文标签: