【问题标题】:how to instantiate/ show a TableViewController如何实例化/显示 TableViewController
【发布时间】:2015-05-22 09:55:49
【问题描述】:

我在这个糟糕的项目上苦苦挣扎了两个星期,但似乎没有任何效果。我必须做一个应用程序,从服务器加载一些单词并将它们呈现为地图上的图钉(MKView)。当用户缩小时,我必须对引脚进行聚类,为此我使用了用 Objective-C 编写的 this-party 库,但我还必须使用按钮制作自定义标注视图。当用户按下所述按钮时,应用程序应该转到 TableViewController,这是我的问题:我无法做到。我在“performSegueWIthIdentifier”之前使用过,效果很好,但现在我收到错误“没有带有'---'标识符的segue”。我知道还有很多其他的线程,但是那里的解决方案都不适合我。此外,我尝试以编程方式实例化 ViewController,但这也不起作用,因为我得到“意外发现 nil ...”,我不知道该怎么做。

我知道我做错了什么,很可能是我如何调用这些函数,但我不知道是什么。这是我到目前为止所尝试的:

在 .xib 文件中我有这个:

import UIKit

class MarkerInfoView: MKAnnotationView {

    @IBOutlet weak var theButton: UIButton!

    @IBAction func readIt(sender: AnyObject) {
        ViewController.goToArticles()
    }

} 

在 ViewController 中:

class func goToArticles(){
        ViewController().reallyGoToArticles()

    }

我这样做是因为我找不到其他方法来调用 performSegueWithIdentifier 或 presentViewController

func reallyGoToArticles(){
        println("let's go!")

        let theArticlesSB = UIStoryboard(name: "Main", bundle: nil)
        let vc = theArticlesSB.instantiateViewControllerWithIdentifier("theArticles") as! articlesViewController
        self.presentViewController(vc, animated: true, completion: nil)
        self.performSegueWithIdentifier("showArticles", sender: self)

    }

我已取消注释这两个选项只是为了向您展示。

我已经上传了项目here

非常感谢!

更新

我忘了说如果我把这条线

self.performSegueWithIdentifier("showArticles", sender: self)

在 viewDidLoad 中有效

【问题讨论】:

    标签: ios xcode swift mkmapview mapkit


    【解决方案1】:

    我查看了您的项目并发现了您的问题。
    MarkerInfoView.swift 中,你调用ViewController.goToArticles() 这是一个类函数:

    class func goToArticles(){
            ViewController().reallyGoToArticles()
        }
    

    这个类函数创建一个新的ViewController 实例,它与情节提要无关(并且不知道转场)。
    你必须从像

    这样的实例方法调用self.reallyGoToArticles()
    func goToArticles(){
                self.reallyGoToArticles()
            }
    

    您必须设法从您的MarkerInfoView 调用现有的ViewController

    编辑:这是实现它的方法
    MarkerInfoView.swift

    class MarkerInfoView: MKAnnotationView {
    
        var vc: ViewController!
        @IBOutlet weak var placePhoto: UIImageView!
        @IBOutlet weak var nameLabel: UILabel!
        @IBOutlet weak var detailsLabel: UILabel!
        @IBOutlet weak var theButton: UIButton!
    
        @IBAction func readIt(sender: AnyObject) {
            vc.goToArticles()
        }
    }
    

    ViewController.swift

    func mapView(mapView: MKMapView!, didSelectAnnotationView annotation: MKAnnotationView!)
        {
            if let pin = annotation.annotation as? CustomPointAnnotation{
    
                if var infoView = UIView.viewFromNibName("MarkerInfoView") as? MarkerInfoView {
                    infoView.nameLabel.text = pin.theTitle
                    infoView.detailsLabel.text = pin.theDetails
                    infoView.vc = self
                    infoView.center = CGPointMake(self.view.bounds.width/2, self.view.bounds.height/2)
    
                    self.view.addSubview(infoView)
    
                } else {
    
                }
            }
        }
    func goToArticles(){
            self.reallyGoToArticles()
        }
    

    【讨论】:

    • 我如何从另一个文件调用方法?这是我从搜索中发现的唯一方法
    • 您必须设置一个属性来保留对您的 ViewController 的引用。当你实例化你的视图时设置这个属性
    • 不客气!请记住,每次实例化一个对象(以任何语言)时,它都会是一个新对象,与其他对象不同,并且不知道应用于其他对象的修改。
    猜你喜欢
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多