Reputation: 23
I'm building a tag-based application and want to call the same function from each tab (ViewController
).
I'm trying to do it in the following way:
#import "optionsMenu.h"
- (IBAction) optionsButton:(id)sender{
UIView *optionsView = [options showOptions:4];
NSLog(@"options view tag %d", optionsView.tag);
}
optionsMenu.h
file:
#import <UIKit/UIKit.h>
@interface optionsMenu : UIView
- (UIView*) showOptions: (NSInteger) tabNumber;
@end
optionsMenu.m
file:
@import "optionsMenu.h"
@implementation optionsMenu
- (UIView*) showOptions:(NSInteger) tabNumber{
NSLog(@"show options called");
UIView* optionsView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
optionsView.opaque = NO;
optionsView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
//creating several buttons on optionsView
optionsView.tag = 100;
return optionsView;
}
@end
The result is that i never get the "show options called" debug message and thus optionsView.tag
is always 0
.
What am i doing wrong?
I understand this is most probably an easy and stupid question, but i am not able to solve it myself.
Any feedback is appreciated.
Upvotes: 0
Views: 91
Reputation: 35686
First thing to note is that this is an instance method (and not a Class method as described in the question title). This means that in order to call this method you should have alloc/init an instance of your Class and send the message to the instance. For example:
// Also note here that Class names (by convention) begin with
// an uppercase letter, so OptionsMenu should be preffered
optionsMenu *options = [[optionsMenu alloc] init];
UIView *optionsView = [options showOptions:4];
Now, if you just want to create a Class method that returns a preconfigured UIView
, you could try something like this (provided that you do not need access to ivars in your method):
// In your header file
+ (UIView *)showOptions:(NSInteger)tabNumber;
// In your implementation file
+ (UIView *)showOptions:(NSInteger)tabNumber{
NSLog(@"show options called");
UIView *optionsView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
optionsView.opaque = NO;
optionsView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
//creating several buttons on optionsView
optionsView.tag = 100;
return optionsView;
}
And finally send the message like this:
UIView *optionsView = [optionsMenu showOptions:4]; //Sending message to Class here
Finally do not forget of course to add your view as a subview in order to display it. I hope that this makes sense...
Upvotes: 3