简体   繁体   中英

How can I manipulate objects in a subView?

Got away from XCode programming for a while, now I'm starting back and feel like I'm starting ALL over. I have a simple example that's driving me crazy. I have created a sub class of UIView called Word1View and added it to my storyboard. I declared it as a property in the main view controller and attached it as an IBOutlet to the main view. I can draw on this view using Core Graphics with no problems (rectangles, lines, etc). Here's the code in the ".h" file. The property is synthesized correctly in the ".m"

#import <UIKit/UIKit.h>
#import "Word1View.h"

@interface Board3ViewController : UIViewController
{
    Word1View *word1View;
}   
@property (nonatomic, strong) IBOutlet Word1View *word1View;
@end

Now, here's the problem. I have added a UILabel (I will be adding UIImageFiles later) in Word1View.h it appears as so:

#import <UIKit/UIKit.h>

@interface Word1View : UIView
{
    UILabel *testLabel;
}
@property (nonatomic, retain) UILabel *testLabel;
@end

This property is also synthesized correctly. The UILabel was added to the view in the main storyboard. I tried to make this an IBOutlet, but could not hook it up as long as it was part of the subview. I tried to access the UILabel in my main view controller as so:

[super viewDidLoad];
[self.view addSubview:word1View];
NSLog(@"Label text is: %@", word1View.testLabel.text);

I don't have any access to that label as evidenced by the output to NSLog which is: Board3[14520:c07] Label text is: (null)

I would like to be able to access the label (change its text, properties etc) from my main View Controller.

Any help is greatly appreciated. Thank you.

You need to instantiate your UILabel and give it some text.

wordView1 = [[UILabel alloc] init];
wordView.text = @"Some Text";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM