【问题标题】:confusion on UIView, its class and UIViewController - xcode iOS objective-cUIView、它的类和 UIViewController 的混淆 - xcode iOS objective-c
【发布时间】:2015-06-22 12:23:38
【问题描述】:
  • 在情节提要中,我将 UIView 拖到了 ViewController 上。
  • 将 UIView 分配给自定义类“myUIView”(即 myUIView.h 和 myUIView.m)
  • 像这样设置 ViewController:

ViewController.h

#import <UIKit/UIKit.h>

@interface ScheduleViewingVC : UIViewController

//for the dragged-out UIView
@property (strong, nonatomic) IBOutlet UIView *myUIViewOutlet; 

//to do some drawings on myUIViewOutlet
@property (strong, nonatomic) myUIView *myUIViewClass; 

@end

ViewController.m

#import "ViewController.h"

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    //resize my UIView into a square
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGRect squareFrame = CGRectMake(0, 0, screenWidth, screenWidth);
    _myUIViewOutlet = [[UIView alloc] initWithFrame:squareFrame];
}
@end

如果我运行这个项目,

- (void)drawRect:(CGRect)rect {}

myUIViewClass.m 内自动运行,因为它链接到 myUIViewOutlet。

现在,我需要从 ViewController 将一些数据发送到链接到 myUIViewOutletmyUIViewClass

从 ViewController 向 myUIViewOutlet 发送数据似乎不正确。 我怎样才能使它正常工作?我应该向 myUIViewClass 发送数据吗?

谁能帮我说清楚?谢谢!

【问题讨论】:

  • 链接到 myUIViewOutlet 的 myUIViewClass 是什么意思?你认为它们是对象吗?
  • UIView(小框架)不应该有自己的类来对自己做一些方法吗?

标签: ios objective-c iphone uiview uiviewcontroller


【解决方案1】:

myUIViewClass 发送数据是不对的。相反,您可以实现一些协议(Delegate、DataSource 类型)的机制。

你可以做的如下:

  1. 在您的 myUIView 类中定义一些自定义协议,例如 myUIViewProtocol
  2. 定义您需要的委托和方法。
  3. 让您的 ViewController 符合此协议并覆盖此方法。

这种方法与 UITableView

希望这会有所帮助。

【讨论】:

  • 我查了一下,发现我可以通过协议从 CHILD -> PARENT 发送.. 我可以同时在两个方向上发送吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2013-01-25
  • 2011-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多