Reputation: 29524
I had asked a question similar to this before, but I have a new problem with it so I've reposted part of the question.
I have this before the interface declaration in my MainView.h
header.
typedef enum { UNKNOWN, CLEAR, NIGHT_CLEAR, CLOUDY, NIGHT_CLOUDY } Weather;
Then I declared it (in my MainView) like this:
Weather weather;
Then made an accessor (and synthesized it):
@property Weather weather;
My question is, how can I use this in MainViewController
without it crashing? I've imported the header for MainView.
I tried to use it like this:
MainView* myView = (MainView*)self.view;
[myView setWeather: CLEAR];
It doesn't throw me any errors in Xcode, but it crashes when the code is run, saying:
-[UIView setWeather:]: unrecognized selector sent to instance *blah*
Am I doing something wrong here?
In my MainViewController:
- (void)viewDidLoad {
[super viewDidLoad];
MainView * drawBox = [[MainView alloc] initWithFrame:(CGRectMake(60, 80, 200, 200))];
drawBox.backgroundColor = [UIColor clearColor];
[self.view addSubview:drawBox];
}
Upvotes: 1
Views: 265
Reputation: 6297
in .m did you synthesize weather? e.g.
@synthesize weather;
so that you'll have the getter and setter created.
[update] After reading your update in viewdidload, it is clear that you are accessing the wrong view because you simply used [self.view addSubView:], this means that self.view is not the MainView, it's just a subview. You can just declare MainView * drawBox as a global and access [drawBox setWeather:] directly.
Upvotes: 0
Reputation: 3572
You added the MainView as a subview of self.view. When you pass setWeather, you are actually calling it on self.view (main view of your view controller, which is a UIView), not on the MainView subview of self.view.
Get the correct MainView object by iterating through self.view.subviews.
Edit: Or just keep an ivar pointing to the MainView subview around, so you can access it anytime.
MainView *drawBox;
in .hdrawBox = [[MainView alloc] initWithFrame:(CGRectMake(60, 80, 200, 200))];
[drawBox setWeather:CLEAR];
when you need toUpvotes: 1
Reputation: 20236
You need to declare the property inside the @interface
section of MainView
, and your Weather weather;
needs to be in the instance variable section of MainView
as well.
UPDATE: In your viewDidLoad, you're adding your MainView as a subview to your VCs view
property. Then when you want to use it, you're getting the VCs view
property, and casting it to a MainView
. You're not getting the view you want when you do that. You're getting the view that's holding your MainView. This is the source of your problem. self.view
as you're referencing it, is a UIView not a MainView. The MainView is a subview of self.view.
Upvotes: 1
Reputation: 31792
As a follow-up to my comment above:
You're probably either saying [[UIView alloc] initWithFrame:...]
somewhere or you have an Interface Builder file with a View object. If you look in the inspector window with that object selected, it will indicate its "Class Identity" as UIView
. Change this to MainView
so the object will have the correct type at runtime.
Upvotes: 3