使用当前模式将数据从一个 uiviewcontroller 发送到另一个导航控制器 [英] Sending data from one uiviewcontroller to another navigationcontroller using present modally

查看:34
本文介绍了使用当前模式将数据从一个 uiviewcontroller 发送到另一个导航控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 swift 的新手,我正在使用故事板并使用导航控制器从一个视图控制器连接到另一个视图控制器.我想将单击的图像的名称发送到下一个视图控制器,该视图控制器从 imageView 以模式连接在情节提要中.我搜索了很多关于将数据从一个视图控制器传输到另一个以模态方式连接到导航控制器的视图控制器,但没有可用的解决方案.请让我知道是否需要任何代码,因为我不知道如何继续.我知道这可能是最愚蠢的问题,但在网上搜索了很多之后发布了这个.

I am new to swift i am using storyboard and have used navigationcontrollers to connect from one viewcontroller to another. I want to send the name of the image clicked to the next viewcontroller which is connected modally in storyboard from the 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.

根据@uraimo 回复进行编辑.

EDIT according to @uraimo reply.

我是否需要为我在故事板上创建的每个转场提供名称?.我在 viewcontrollerA 上有 2 个固定图像,我在每个图像上都放置了一个具有透明背景且没有文本的 uibutton,然后按 ctrl 拖动到 viewcontrollerB 的导航控制器,以便 以模态呈现 并展开后退按钮,即 UIBarButtonItem 到 viewcontrollerA通过ctrl拖动viewcontrollerB的后退按钮退出viewcontrollerB并展开.

Do i need to provide name to every segue i created on storyboard?. 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 i.e. UIBarButtonItem to viewcontrollerA by ctrl drag the back button of viewcontrollerB to exit of the viewcontrollerB and unwinding it.

这就是我如何创建从 viewcontrollerA 的 3 个图像中的任何图像单击到 viewcontrollerB 并在单击 viewcontrollerB 的后退按钮时返回到 viewcontrollerA 的导航.如果我做错了什么,请告诉我,您的 prepareForSegue 代码是否有助于完成我的任务.

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. Please let me know if i am doing anything wrong and will your prepareForSegue code be useful in accomplishing my task.

推荐答案

基本上,无论是使用 IB 还是以编程方式进行时,您都必须在执行转场(或控制器通过代码呈现).

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).

在您的情况下,只需设置图像名称(您的自定义视图控制器类 YourViewController 应具有特定的 String 属性来保存此值)覆盖当前视图中的 prepareForSegue控制器类:

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:

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.

但在您的情况下,您需要单击图像的名称,并且只能通过 UIGestureRecognizer 添加单击事件到 UIImageView 来获得.

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.

因此,您需要一个 uigesturerecognized,点击后将执行您创建的转场.而且,您将无法获取图像资产的名称(您使用 imageNamed: 创建 UIImage 的名称),因为它不再可用,并且您必须使用 accessibilityIdentifier.

So, you'll need a uigesturerecognized that on click will perform the segue you've created. 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.

这让一切变得更加复杂,似乎大部分都可以通过图形方式来完成 此处此处(但具体怎么做不是很清楚),但通常是通过代码来完成的.

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)
}

并改变这一点:

yourController.imageName= <name here>

为此:

yourController.imageName= currentImage

更新:

  • 我是否需要为我在故事板上创建的每个转场提供名称?

是的,这是识别它们的唯一方法,每个 UIStoryboardSegue 都有一个标识符.但是请记住,segue 并不是从一个控制器转到另一个控制器的唯一方法,如果您完全以编程方式(没有 segue)执行此操作,您通常将其称为presentViewController".Segues 是一个故事板概念.

Yes, it's the only way to identify them, every UIStoryboardSegue has an identifier. 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". Segues are a storyboard concept.

同样,关于 segue 名称/标识符,您直到现在才需要它,因为您从未从代码中引用过该 segue,prepareForSegue 和 performSegueWithIdentifier 都需要它.只需选择 segue 并在右侧检查器窗格中为其命名.

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. Just select the segue and give it a name on the right inspector pane.

您描述的结构似乎没问题,唯一的问题是我不太确定 UIButton 是否真的需要,请尝试从 imageview 或直接从 viewcontroller 到目标视图控制器的模态转场.

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.

更新 2:

如果您刚开始并需要一门免费课程来教您基础知识并让您构建一些有趣的 ios 应用程序,我推荐 hackingwithswift.

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.

这篇关于使用当前模式将数据从一个 uiviewcontroller 发送到另一个导航控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆