Question

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];
}
Was it helpful?

Solution

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.

  1. declare MainView *drawBox; in .h
  2. in viewDidLoad, init as drawBox = [[MainView alloc] initWithFrame:(CGRectMake(60, 80, 200, 200))];
  3. simply call [drawBox setWeather:CLEAR]; when you need to

OTHER TIPS

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top