简体   繁体   English

使用当前模式将数据从一个 uiviewcontroller 发送到另一个导航控制器

[英]Sending data from one uiviewcontroller to another navigationcontroller using present modally

I am new to swift i am using storyboard and have used navigationcontrollers to connect from one viewcontroller to another.我是 swift 的新手,我正在使用故事板并使用导航控制器从一个视图控制器连接到另一个视图控制器。 I want to send the name of the image clicked to the next viewcontroller which is connected modally in storyboard from the imageView.我想将单击的图像的名称发送到下一个视图控制器,该视图控制器从 imageView 以模式连接在情节提要中。 I searched lot about transferring data from oneviewcontroller to another viewcontroller connected with navigationcontroller modally but no solution was available.我搜索了很多关于将数据从一个视图控制器传输到另一个以模态方式连接到导航控制器的视图控制器,但没有可用的解决方案。 Please let me know if any of the code is required as i dont know how to go ahead with it.请让我知道是否需要任何代码,因为我不知道如何继续。 I know this might be silliest question but posting this after searching a lot on net.我知道这可能是最愚蠢的问题,但在网上搜索了很多之后发布了这个问题。

EDIT according to @uraimo reply.根据@uraimo 回复进行编辑。

Do i need to provide name to every segue i created on storyboard?.我是否需要为我在故事板上创建的每个 segue 提供名称?。 I have 2 fixed images on viewcontrollerA and i have placed a uibutton with transparent background and no text on each of them and then ctrl drag to navigation controller of viewcontrollerB for presenting modally and unwinding the backbutton ie UIBarButtonItem to viewcontrollerA by ctrl drag the back button of viewcontrollerB to exit of the viewcontrollerB and unwinding it.我在 viewcontrollerA 上有 2 个固定图像,我在每个图像上放置了一个具有透明背景且没有文本的 uibutton,然后按 ctrl 拖动到 viewcontrollerB 的导航控制器以进行模态呈现并展开后退按钮,即 UIBarButtonItem 到 viewcontrollerA 通过 ctrl 拖动后退按钮viewcontrollerB 退出 viewcontrollerB 并展开它。

This is how i have created navigation from any of the image click out of 3 images of viewcontrollerA to viewcontrollerB and back to viewcontrollerA on back button click of viewcontrollerB.这就是我如何创建从 viewcontrollerA 的 3 个图像中的任何图像单击到 viewcontrollerB 并在单击 viewcontrollerB 的后退按钮时返回到 viewcontrollerA 的导航。 Please let me know if i am doing anything wrong and will your prepareForSegue code be useful in accomplishing my task.如果我做错了什么,请告诉我,您的 prepareForSegue 代码是否有助于完成我的任务。

Basically, both using IB or when you do it programmatically, you have to configure your new viewcontroller with all the data it needs before the segue is performed (or the controller is presented via code).基本上,无论是使用 IB 还是以编程方式进行时,您都必须在执行 segue 之前使用它需要的所有数据配置新的视图控制器(或控制器通过代码呈现)。

In your case, just set the image name (your custom view controller class YourViewController should have a specific String property to hold this value) overriding prepareForSegue in the current view controller class:在您的情况下,只需设置图像名称(您的自定义视图控制器类YourViewController应该具有特定的 String 属性来保存此值)覆盖当前视图控制器类中的prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "yourModalSegueIdentifier" {
            let imgName= (sender as! UIImageView)
            let destination = segue.destinationViewController as UINavigationController
            let yourController = destination.topViewController as YourViewController
            yourController.imageName= <name here>
        }
}

This solves the passing data question.这就解决了传递数据的问题。

But in your case, you need the name of the clicked image, and that can be only obtained adding a click event through a UIGestureRecognizer to the UIImageView .但是在您的情况下,您需要单击图像的名称,并且只能通过 UIGestureRecognizer 将单击事件添加到UIImageView来获得。

So, you'll need a uigesturerecognized that on click will perform the segue you've created.因此,您需要一个 uigesturerecognized,点击时将执行您创建的转场。 And also, you will not be able to get the name of the image asset (the one you use the creating an UIImage using imageNamed: ) because it's not available anymore, and yo'll have to use the accessibilityIdentifier .而且,您将无法获得图像资产的名称(您使用imageNamed:创建UIImage imageNamed: ),因为它不再可用,您必须使用accessibilityIdentifier

This makes everything way more complicated, it seems it could be done for the most part graphically here and here (but it's not really clear how to do it), but it's usually done via code.这使一切变得更加复杂,似乎可以在此处此处以图形方式完成大部分工作(但尚不清楚如何做到这一点),但通常通过代码完成。

Simplifying it a bit using a global variable:使用全局变量稍微简化一下:

var currentImage = ""

func viewDidLoad(){
    ...
    ...
    let aTap = UITapGestureRecognizer(target: self, action: Selector("imageTapped:"))
    aTap.numberOfTapsRequired = 1

    //For every image, configure it with recognizer and accessibilityId:
    firstImage.userInteractionEnabled = true
    firstImage.addGestureRecognizer(aTap)
    firstImage.accessibilityIdentifier = "firsImage"
    ...
}

func imageTapped(recognizer:UITapGestureRecognizer) {
    let imageView = recognizer.view as! UIImageView
    currentImage = imageView.accessibilityIdentifier
    self.performSegueWithIdentifier("yourModalSegueIdentifier", sender: self)
}

And change this:并改变这一点:

yourController.imageName= <name here>

to this:对此:

yourController.imageName= currentImage

Update:更新:

  • Do i need to provide name to every segue i created on storyboard?我是否需要为我在故事板上创建的每个转场提供名称?

Yes, it's the only way to identify them, every UIStoryboardSegue has an identifier.是的,这是识别它们的唯一方法,每个UIStoryboardSegue都有一个标识符。 But remember, segues are not the only way to go from a controller to another, if you do it completely programmatically (no segues) you usually call "presentViewController".但是请记住,segue 并不是从一个控制器转到另一个控制器的唯一方法,如果您完全以编程方式(没有 segue)执行此操作,您通常将其称为“presentViewController”。 Segues are a storyboard concept. Segues 是一个故事板概念。

Again, regarding the segue name/identifier, you didn't need it until now because you never referenced that segue from your code, you need it for both prepareForSegue and performSegueWithIdentifier.同样,关于转场名称/标识符,您直到现在才需要它,因为您从未从您的代码中引用该转场,因此 prepareForSegue 和 performSegueWithIdentifier 都需要它。 Just select the segue and give it a name on the right inspector pane.只需选择 segue 并在右侧检查器窗格中为其命名。

The structure you describe seems ok, the only thing it's that i'm not so sure that the UIButtons are really needed, try with a modal segue from the imageview or directly from the viewcontroller to the destination view controller.您描述的结构似乎没问题,唯一的问题是我不太确定 UIButtons 是否真的需要,请尝试从 imageview 或直接从 viewcontroller 到目标视图控制器的模式 segue。

Update 2:更新 2:

If you are starting and need a free course that will teach you the basics and also make you build a few interesting ios apps i recommend hackingwithswift .如果您开始并需要一门免费课程来教您基础知识并让您构建一些有趣的 ios 应用程序,我建议您使用hackingwithswift

check out how I did this看看我是怎么做到的

// In a storyboard-based application, you will often want to do a little preparation before navigation
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

      super.prepare(for: segue, sender: sender)

      switch(segue.identifier ?? "") {

      case "AddItem":
          let destination = segue.destination as? UINavigationController
          guard let itemViewController = destination?.topViewController as? ItemViewController else {
              fatalError("Unexpected destination: \(segue.destination)")
          }
          itemViewController.collection = collection

      case "EditCollection":
          guard let collectionViewController = segue.destination as? EditCollectionViewController else {
              fatalError("Unexpected destination: \(segue.destination)")
          }
          collectionViewController.collection = collection

      default:
          fatalError("Unexpected Segue Identifier; \(segue.identifier)")
      }
  }

暂无
暂无

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

相关问题 以编程方式从 UITabBarController 呈现模态 UIViewController - Present Modally UIViewController From UITabBarController Programmatically 在另一个UIViewController中 - Present UIViewController in another one NavigationController:将推送的UIViewController替换为另一个UIController - NavigationController: Replacing a pushed UIViewController with another one 在另一个UIViewController中使用一个UIViewController中的UIView? - Using an UIView from one UIViewController in another UIViewController? 如何模态呈现标准的UIViewController - How to present a standard UIViewController modally 我可以在屏幕外以模态呈现UIViewController吗? - Can I present a UIViewController modally offscreen? 如何在嵌入在UINavigationController中的另一个Storyboard上以模块化方式呈现UIViewController? - How can I modally present a UIViewController on another Storyboard that's embedded in a UINavigationController? 如何通过UITapGestureRecognizer将数据从一个UIViewController传递到另一个UIViewController - How to pass data from one UIViewController to another one through UITapGestureRecognizer 如何以模态方式呈现的UIViewController的UIView总是出现在屏幕的底部? - How to have a UIView of a UIViewController that will be presented modally always be present at the bottom of the screen? 使用委托从 UIView 子类向 UIViewController 发送数据 - Sending Data to UIViewController From UIView Subclass With Delegate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM