需要解释:在 UIViewController 和 UIView 之间传递数据的概念 [英] Explanation wanted: Concept of passing data between UIViewController and UIView

查看:49
本文介绍了需要解释:在 UIViewController 和 UIView 之间传递数据的概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了很多在视图控制器之间传递数据"的问题.但是我好像不能理解传递数据的概念.

I have browsed through a lot of the "pass data between view controller" questions. But I can't seem to grasp the concept of passing data.

为什么将用户或系统生成的值从一个视图转换为另一个视图如此麻烦?我是 Objective-C 的新手,但我以前做过一些编程,所以我对所有术语并不陌生.

Why is it such a hassle to get user- or system generated value's from one view to another? I'm new to Objective-C, but I have done some programming before in my life so I'm not unfamiliar with all the terminologies.

我已经阅读了关于委托、协议、单例和 segue 的内容.我曾尝试使用示例文件进行试验,但无法根据我的需要回收代码,也不了解传递数据需要哪些过程.

I have read about delegate's, protocols, singletons and segue's. I have tried to experiment with example files but could not recycle the codes to my needs nor do I understand what processes it takes to pass data.

我想学习的是如何将所有生成的数据从 1 个 UIViewController 传递到多个 UIViews.我生成的所有值都需要能够即时更新并且无需视图切换即可访问,也不需要按下按钮来更新/传递数据槽.

What I'm trying to learn is how I can pass all the generated data from 1 UIViewController to multiple UIViews. All the value's I generate need to be able to update instantaneously and be accesible without requiring a view switch nor does a button press be required to update/pass the data trough.

谁能一劳永逸地解释一下如何在不同的头文件和 UIViews 之间传递数据.这让我发疯了,我只希望数据在那里并且可以随时随地访问我需要的数据.

Can someone please explain me once and for all how to pass data between different header files and UIViews. It is driving me nuts, I just want the data to be there and accessible whenever and where ever I need it.

这就是我想要创造的,我得到了三个滑块,每个滑块代表一个轴.

This is what i'm trying to create, I got three sliders each representing an axis.

用户可以根据滑块使用的值创建两个关键帧.

The user can create two keyframes based on the values they use with the sliders.

这些值被保存(在 UIViewControler.m 中)并在屏幕上创建一个视觉关键帧(UIImage).关键帧 1 和关键帧 2,这些关键帧将用作 UIBezierCurve 的 CGPoints.

These values get saved (in the UIViewControler.m) and create a visual keyframe(UIImage) on screen. Keyframe 1 and keyframe 2, these keyframes will be used as CGPoints for a UIBezierCurve.

话虽如此,我将一个 UIView 拖入我的 UIViewController 并将 UIView 链接到我的BezierGraphView.h".

That being said, I dragged an UIView into my UIViewController and linked the UIView to my "BezierGraphView.h".

现在我需要将这些关键帧点传递给 BezierGraphView.我该怎么做?

Now i need to pass these keyframe points to the BezierGraphView. How would i do this?

推荐答案

在 iOS 中,很多时间都花在了 ViewController 上.视图控制器非常重要,主要有两个原因:

In iOS, a lot of time is spent in ViewControllers. View Controllers are vitally important for two main reasons:

  • 他们引用了屏幕上的视图对象(即 self.loginButton 指的是视图控制器有权访问的 UIButton)
  • 他们了解数据模型.(即存在对与屏幕相关的数据的引用)

有两种不同类型的数据传递.将数据对象从 UIViewController 传递到不同的 UIViewController [这里解释得很清楚:传递数据对象].然后是 UIViewController 使用数据对象在屏幕上显示信息的方法.

Theres two different types of data passing. Theres passing data objects from UIViewController to a different UIViewController [explained beautifully here: Passing Data Objects]. Then theres the method where a UIViewController uses the data object to display information on the screen.

所以为了在屏幕上有一个 UIViewController 效果,你需要:

So in order to have a UIViewController effect something on the screen you need to:

  1. 拥有一个数据对象(一个数组、一个自定义对象、任何真正的东西)
  2. 引用视图(UILabel、UIButton 等)

例如,这里是如何生成和使用临时数据在屏幕上显示信息的方法.假设有一个 User 对象和一个 UserNameLabel;

For example here is how you can generate and use temp data to display information on the screen. Assume there is a User object and a UserNameLabel;

- (id)init {
     if (self = [super init]) {
        self.user = [User newUserWithTempData];
     }
     return self;
 }

然后在 viewDidLoad 方法中:

Then in the viewDidLoad method :

- (void)viewDidLoad
{
     [super viewDidLoad];
     self.userNameLabel.text = self.user.nameString; 
}

这会处理使用数据来影响屏幕上的视图.如果您问的是如何在不同的视图控制器之间共享 User 对象,那么这是一个不同的问题,如果您愿意,我可以解释.

This handles using data to affect views on the screen. If you're asking about how to share the User object between different View Controllers, then that is a different issue which I can explain if you'd like.

我假设您的问题是询问如何从情节提要中获取对视图的引用.

I'm assuming your question is asking how to get a reference to a view from storyboard.

我将逐步解释这张图片中的所有内容,因为它有望引导您完成将所有内容组合在一起的步骤.我将根据编号箭头所指的内容将其分解.我们开始:

I'll explain everything in this image step by step as it will hopefully walk you through the steps to put it all together. I'll break it up based on what the numbered arrows refer to. Here we go:

箭头 1

箭头 1 指向 UITableViewCell 子类的 xib 的文档大纲.

Arrow 1 is pointing to the Document Outline of a xib of a UITableViewCell subclass.

注意: CircleView 是 UIView 的自定义子类.使用以下实现:

Note: CircleView is a custom subclass of UIView. With the following implementation:

#import "CircleView.h"

@implementation CircleView

- (void)drawRect:(CGRect)rect{
    // CUSTOM DRAWING METHOD HERE
}

@end

箭头 1 指向屏幕上的自定义 CircleView 视图.为此,我向屏幕添加了一个 UIView,然后更改了类.

Arrow 1 is pointing to the custom CircleView view on the screen. To do this, I add a UIView to the screen and then change the class.

箭头 2

箭头 2 指向如何将 Storyboard 或 Interface Builder 中的视图更改为具有自定义实现的自定义视图.在这里,只需选择视图并在其上打开文件检查器.然后找到这个选项卡,把自定义类从 UIView 改成 MyCustomView.

Arrow 2 is pointing to how to change a view in Storyboard or Interface Builder to a custom view that has a custom implementation. Here, simply have the view selected and open the file inspector on it. Then find this tab and change the custom class from UIView to MyCustomView.

箭头 3

箭头 3 指向与 Interface Builder 中的自定义视图相关的属性.

Arrow 3 is pointing to the property that relates to the custom view in the Interface Builder.

代码如下:

#import <UIKit/UIKit.h>
@class CircleView;
@interface CreateNewQuestionCell : UITableViewCell
     @property (nonatomic, weak) IBOutlet CircleView *circleView;
@end

在此处使用您创建的自定义类声明属性.确保有 IBOutlet,以便 Xcode 可以允许下一步工作:)

Here is where you declare the property, using the custom class that you created. Make sure to have IBOutlet so that Xcode can allow the next step to work :)

箭头 4

箭头 4 指向一个指示符,该指示符告诉您故事板视图中的属性是否连接到类中的属性.这就是您确保一切正常的方式.

Arrow 4 is pointing to an indicator that tells you whether or not properties are on a view from storyboard are connected to the property in the class. This is how you ensure everything is wired up.

有 2 种方法可以做到这一点,我将展示快速方法,但如果您好奇,请告诉我,我会添加更多.

Theres 2 ways to do this, I'll show the quick way, but if you're curious let me know and I'll add more.

  • 单击指示器并拖动到您创建并释放的自定义视图.[顺便说一句,要打开 2 个屏幕,请打开 Assistant Editor ]

这是一个快照:

完成

现在,当我运行应用程序时,该视图现在是一个圆形视图,一个在该区域绘制一个阴影圆圈的自定义类.

Now when I run the app, that view is now a Circle View, a custom class that draws a shaded circle in the area.

此时,您可以在 viewDidLoad 函数中更改您需要的任何内容.

And at this point, you can change anything you need to in the viewDidLoad function.

这篇关于需要解释:在 UIViewController 和 UIView 之间传递数据的概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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