• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - iOS 从 View Controller 访问 View 的变量

[复制链接]
菜鸟教程小白 发表于 2022-12-13 08:04:59 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在 iOS 上进行开发,并且正在以编程方式构建我的 View 。我注意到,当我尝试从 View Controller 访问必须在 View 中更改的变量时,它们为空。我将发布 View 及其 View Controller :

RootViewController.h

 #import <UIKit/UIKit.h>

@class RootView;

@interface RootViewController : UIViewController {
    RootView *rootView;
}

@property (nonatomic,retain) RootView *rootView;


@end

RootViewController.m

    #import "RootViewController.h"
#import "RootView.h"

@implementation RootViewController

@synthesize rootView;

- (id)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}



- (void)dealloc
{
    [rootView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)loadView{
    RootView *rootV = [[RootView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
    rootV.rootViewController = self;
    self.view = rootV;
    [rootV release];
}

- (void)viewDidLoad{
    NSLog(@"TEXT: %@",self.rootView.label.text);
    self.rootView.label.text=@"HELLO!";
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [self setRootView:nil];
}

- (BOOL)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@结束

RootView.h

   #import <UIKit/UIKit.h>

@class RootViewController;

@interface RootView : UIView {
    RootViewController *rootViewController;
    UILabel *label;
}

@property (nonatomic,assign) RootViewController *rootViewController;
@property (nonatomic,retain) UILabel *label;


@end

RootView.m

   #import "RootView.h"
#import "RootViewController.h"

@implementation RootView
@synthesize rootViewController;
@synthesize label;

- (id)initWithFrameCGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //Create the label
        UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100,100, 50)];
        //Set the font bold 
        testLabel.font = [UIFont boldSystemFontOfSize:20.0];
        //Set the backgroundcolor of the label to transparent
        testLabel.backgroundColor = [UIColor clearColor];
        //Set the text alignment of the text label to left
        testLabel.textAlignment = UITextAlignmentLeft;
        //Set the text color of the text label to black
        testLabel.textColor = [UIColor blackColor];
        testLabel.text = @"01:30";

        self.label = testLabel;

        [self addSubview:label];
        [testLabel release];
    }
    return self;
}


- (void)dealloc
{
    [label release];
    rootViewController = nil;
    [super dealloc];
}

@end

我更改了代码,但似乎无法正常工作.....

好的解决了,我忘了这行“self.rootView = rootV;”



Best Answer-推荐答案


直到 -initRootView 方法返回后,您的 View 才知道它的 Controller 是什么,但您正试图从该方法中使用 Controller 。

也就是说,如果您按照通常的 Cocoa Touch 模式来创建 View Controller ,那会更好。 View Controller 应该懒惰地创建它们的 View ,也就是说它们推迟 View 的创建和初始化,直到调用 -loadView 方法。您可以覆盖 -loadView 来创建您的 View ,也可以覆盖 -viewDidLoad 来执行任何需要在创建 View 后完成的设置工作。

此外,通常不建议 View 了解其 Controller 。 Controller 应该告诉 View 该做什么,而不是相反。如果您需要 View 向 Controller 发送一些信息,您通常将 Controller 作为 View 的委托(delegate)提供给 View 。但是如果你只需要 View Controller 能够找到一些 subview ,比如你的标签,那么在容器 View 中提供一些访问器可能是个好主意(这样 View Controller 就可以说类似 self.view.label.text = @"some text";。另一种选择是将 subview 的标签属性设置为某个唯一值,并让 Controller 使用它来查找 subview 。

关于iphone - iOS 从 View Controller 访问 View 的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6456961/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap