我有2个视图控制器。 viewController1有一个名为nameLabel的UILabel,我希望在另一个类viewController2中设置。
我尝试从viewController2调用此代码。
Content1ViewController *viewController1 = [Content1ViewController new];
viewController1.nameLabel.text = @"HELLO";
NSLog(@"%@",viewController1.nameLabel);
但是,当我调用代码时,viewController1 nameLabel不会改变? NSLog还返回“null”??有人能告诉我为什么会发生这种情况,以及我如何从另一个类中更改nameLabel?谢谢!
答案 0 :(得分:2)
手动创建实例时,不会初始化控件。这是在视图控制器生命周期的后期阶段完成的。
如果我没有弄错的话,您将看到初始化控件的第一个事件是viewDidLoad
您可以做的是添加一个NSString
属性名称(让我们说)nameLabelText
并在nameLabel.text = nameLabelText;
上执行viewDidLoad
答案 1 :(得分:0)
您需要使用窗口上的viewController的相同实例。如果要将viewController2
推入导航控制器堆栈,则从导航堆栈中获取viewController1
的实例,然后尝试设置标签。
如果您使用的是现有模式,请使用presentingViewController
viewController2
实例来访问viewController1
的实例
viewController1 *controller = [viewController2 presentingViewController];
[controller.nameLabel setText:@"WhatEver"];
答案 2 :(得分:0)
您在哪里为namelabel设置了属性? 您是否使用Interface Builder(XIB或Storyboard)或者您是否已在代码中完成所有操作? 如果你在InterfaceBuilder中做到了
[Content1ViewController new]
是错误的做法。 对于XIB-Files,您应该使用
initWithNibName:(NSString *)nibName
bundle:(NSBundle *)nibBundle
如果您的ViewController已在故事板中定义,则应使用
instantiateViewControllerWithIdentifier:(NSString *)identifier
来自UIStoryboard
。
但是,正如Claudio Redi已经说过,你能够访问UILabel的最早点将在viewDidLoad
如果代码中的所有内容都应该放
[self setNameLabel:[[UILabel alloc] initWithFrame:CGRectMake(50.0f,50.0f,100.0f,100.0f)];
在init
- ViewController的方法。
答案 3 :(得分:0)
//
// ViewController.m
// UIPageViewControllerDemo
//
// Created by YunYi1118 on 15/5/20.
// Copyright (c) 2015年 Xiaoyi. All rights reserved.
//
#import "ViewController.h"
#import "MoreViewController.h"
@interface ViewController () <UIPageViewControllerDataSource, UIPageViewControllerDelegate>
@property (strong, nonatomic) UIPageViewController *pageController;
@property (strong, nonatomic) NSArray *pageContent;
@end
@implementation ViewController
@synthesize pageContent = _pageContent;
@synthesize pageController = _pageController;
- (void)viewDidLoad {
[super viewDidLoad];
[self createContentPages];
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin] forKey:UIPageViewControllerOptionSpineLocationKey];
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
_pageController.dataSource = self;
_pageController.view.frame = self.view.bounds;
MoreViewController *initialViewController = [self viewControllerAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:_pageController];
[self.view addSubview:_pageController.view];
}
- (MoreViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (self.pageContent.count == 0 || index >= self.pageContent.count) {
return nil;
}
MoreViewController *dataViewController = [[MoreViewController alloc] init];
dataViewController.dataObject = [self.pageContent objectAtIndex:index];
return dataViewController;
}
- (void)createContentPages
{
NSMutableArray *pageStrings = [NSMutableArray array];
for (int i = 1; i < 11; i++) {
NSString *contentString = [[NSString alloc] initWithFormat:@"Chapter%d, This is the page %d of content displayed using UIPageViewController ",i,i];
[pageStrings addObject:contentString];
}
self.pageContent = [NSArray arrayWithArray:pageStrings];
}
#pragma mark - delegate
- (NSUInteger)indexOfViewController:(MoreViewController *)viewController
{
return [self.pageContent indexOfObject:viewController.dataObject];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(MoreViewController *)viewController];
if (index == 0 || index == NSNotFound) {
return nil;
}
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(MoreViewController *)viewController];
if (index == NSNotFound) {
return nil;
}
index++;
if (index == self.pageContent.count) {
return nil;
}
return [self viewControllerAtIndex:index];
}
@end