简体   繁体   English

如何在情节提要中将视图控制器连接到其委托?

[英]How can I connect a view controller to its delegate in a storyboard?

I'm trying to figure out how to get a reference to another view controller (delegate) in a project that uses a storyboard. 我试图弄清楚如何在使用情节提要的项目中获得对另一个视图控制器(委托)的引用。 The delegate has been nil in all of my attempts so far. 到目前为止,我的所有尝试都没有使代表成功。

I have a project right now that's pretty simple and has just three view controllers. 我现在有一个非常简单的项目,只有三个视图控制器。

  • MainScreenViewController: The root view controller, and contains the buttons that segue into the other view controllers (presented modally). MainScreenViewController:根视图控制器,并包含连接到其他视图控制器(以模态显示)的按钮。

  • PhotoScanViewController: Has the user pick/take a photo, which it then scans to produce some data. PhotoScanViewController:让用户拾取/拍摄照片,然后对其进行扫描以生成一些数据。 The user can can then save the photo and the associated data. 然后,用户可以保存照片和相关数据。

  • PhotoListTableViewController: Contains the list of photos with their data that the user has saved. PhotoListTableViewController:包含照片列表以及用户已保存的数据。

From this, I need PhotoScan to send the data over to PhotoList when a user saves a photo. 由此,当用户保存照片时,我需要PhotoScan将数据发送到PhotoList。 I figured that I should do this using the delegate pattern, with PhotoList being a delegate that receives messages from PhotoScan. 我认为我应该使用委托模式来执行此操作,其中PhotoList是从PhotoScan接收消息的委托。 I just can't seem to figure out how to get a reference to PhotoList from PhotoScan when I'm using a storyboard. 当我使用情节提要时,我似乎无法弄清楚如何从PhotoScan获得对PhotoList的引用。 故事板层次 What I've tried: 我尝试过的

1) From looking at similar questions, I saw that most of the time people set delegates when prepareForSegue:sender: is called. 1)通过查看类似的问题,我看到大多数时候人们在调用prepareForSegue:sender:时设置代表。 However, this does not work in my case, since PhotoScan and PhotoList are not connected by a segue in the view hierarchy. 但是,在我的情况下,这不起作用,因为PhotoScan和PhotoList没有通过视图层次结构中的segue连接。

2) I've tried making the delegate an IBOutlet to wire up in the storyboard, but it seems that I cannot connect outlets from one scene into another. 2)我尝试使该代表成为IBOutlet以便连接到情节提要中,但是似乎我无法将插座从一个场景连接到另一个场景。

3) Since I couldn't connect it inside the storyboard, I tried to programmatically connect it inside the AppDelegate. 3)由于无法将其连接到情节提要中,因此我尝试以编程方式将其连接到AppDelegate中。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // Get the storyboard instance
    let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())

    // Get references to controllers
    let photoScanViewController = storyboard.instantiateViewControllerWithIdentifier("PhotoScanViewController") as PhotoScanViewController
    let photoListTableViewController = storyboard.instantiateViewControllerWithIdentifier("PhotoListTableViewController") as PhotoListTableViewController

    // Set delegate
    photoScanViewController.photoDelegate = photoListTableViewController

    return true
}

This didn't work either: photoListDelegate was still nil when I ran the app. 这也不起作用:当我运行应用程序时, photoListDelegate仍然为零。 I think what's happening is that instantiateViewControllerWithIdentifier: is giving me new instances for the view controllers rather than the ones actually being used when the app runs. 我认为发生了什么事, instantiateViewControllerWithIdentifier:给我提供了视图控制器的新实例,而不是应用程序运行时实际使用的实例。

At this point, I don't know what else to do other than changing up my view hierarchy so that I can use prepareForSegue:sender: and set the delegate there like everyone else. 此时,除了更改视图层次结构之外,我不知道还能做什么,以便可以使用prepareForSegue:sender:并将委托设置为其他所有人。

Your decision of delegation is correct pattern -- only to the wrong view controller. 您的委派决定是正确的模式-仅适用于错误的视图控制器。

When the user saves a photo from PhotoScanViewController, have all the data data delegate it back to the MainScreenViewController. 当用户从PhotoScanViewController保存照片时,请将所有数据数据委托给MainScreenViewController。 Now that you have the data, when the user taps on the "list" button, supply the newly fetched data to the PhotoListViewController. 现在您已经有了数据,当用户点击“列表”按钮时,将新获取的数据提供给PhotoListViewController。 This should work out for you. 这应该为您解决。

Here is some code to help you out: 这是一些代码可以帮助您:

class ImageScannerViewController {
    var delegate:ImageScannerViewControllerDelegate?
    var imageData:NSData? //
    func getScannedImageData() -> NSData {

    //Method to get imageData here

    }
    @IBAction didTapSaveButton(sender:AnyObject?) {
      imageData = self.getScannedImageData()
      self.delegate?.imageScannerViewController(self,imageData:imageData!))

    }
}

protocol ImageScannerViewControllerDelegate {
    func imageScannerViewController(imageScanerViewController, didSaveImageWithImageData, imageData:NSData)
}

In your MainScreenViewController 在您的MainScreenViewController中

class MainScreenViewController:UIViewController,ImageScannerViewControllerDelegate {
    var imageData:NSData?
    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
       if (segue.identifier == "scannerViewControllerSegue") {
        // pass data to next view
           let scannerViewController = segue.destinationViewController as ImageScannerViewController
           scannerViewController.delegate = self
       }
   }
    func imageScannerViewController(imageScanerViewController, didSaveImageWithImageData, imageData:NSData) {
      self.imageData? = imageData
    }

}

暂无
暂无

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

相关问题 如何从Storyboard设置Split View Controller Delegate? - How to set Split View Controller Delegate from Storyboard? 如何使用Storyboard将两个视图控制器连接到一个视图控制器? - How to connect two view controller to one view controller using Storyboard? 如何使用LGSideMenuController推送带有情节提要标识符的视图控制器? - How can I push view controller with storyboard identifier using LGSideMenuController? 来自app delegate的故事板视图控制器? - Present storyboard view controller from app delegate? 如何从情节提要中的子视图(另一个视图控制器)内的按钮推动新的视图控制器 - How can I push a new View Controller from a button within a subView (another View Controller) in Storyboard xcode,如何使用情节提要参考连接内部视图控制器? - xcode, how to use storyboard reference connect inner view controller? Xamarin iOS:如何将故事板中的视图连接到控制器? - Xamarin iOS: How to connect a view from a storyboard to a controller? 在情节提要中,如何为视图控制器的子视图分配不同的委托/控制器m文件? - In storyboard, how to assign a view controller's subview a different delegate/controller m file? 如何将数据从集合视图单元传递到新的视图控制器(以编程方式且没有情节提要)? - How can I passed data from collection view cell to new view controller (programmatically and without storyboard)? 如何在故事板中连接UIPickerView数据源和委托 - How to connect UIPickerView datasource and delegate in storyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM