I have a class which is a viewcontroller and it contains:
-(void)testNSTimer
{
[UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionCurveEaseIn
animations:^{ self.view.welcomeBackLabel alpha = 0;} //welcomeBackLabel does not exist.
completion:nil];
}
However welcomeBackLabel is not available in this circumstance because it is created in the controller's view AKA self.view as shown in the following code:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
welcomeBackLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, -20, 100, 120)]; //welcomeBackLabel is not available for other classes to use, but I want it to be.
[welcomeBackLabel setText:#"Welcome"];
[welcomeBackLabel setTextAlignment:NSTextAlignmentCenter];
UIFont *welcomeFont = [UIFont fontWithName:#"Helvetica-Bold" size:20];
welcomeBackLabel.font = welcomeFont;
[welcomeBackLabel setTextColor:[UIColor whiteColor]];
[welcomeBackLabel setAlpha:0.65];
[self addSubview:welcomeBackLabel];
}
I created a property however it is not detected by the viewcontroller's class even after importing the view's .h file. Can anybody should me the various ways I could have welcomeBackLabel be usable in my controller class?
EDIT:
For example, in my view's .h I have:
#property(nonatomic, retain) UILabel *welcomeBackLabel;
and I have
#import "view.h"
in my controller.m but its not detecting it as a usable property o.o
EDIT2:
Ok so if I have HomeScreenView *testView = [[HomeScreenView alloc]init]; then YES i can use the property fine ala testView.welcomebackLabel.etc . It just does not work when I have set the view for the controller using [self setView:testView] and then use self.view.welcomeBackLabel... Why is this?
In a UIViewController, self.view is returned as a UIView. If you "know" that it is a particular subclass of UIView, you need to cast it so you can access methods from the subclass. You could inline this, but for clarity:
MyUIView *v = self.view;
[v myMethod];
Or if you want inline:
[(MyUIView *) self.view myMethod];
If it is sometimes the right subclass you can test:
UIView *v = self.view;
if([v isKindOfClass:[MYUIView class]]) {
[(MyUIView *) self.view myMethod];
}
Create an #property in the .h file of your class and it should be accessible from other classes well.
Related
I want to create another UIView / UIButton on an UIButton so I'm getting its' frame and use it in declaration of another UIView but It creates like below screenshot (with blue view). By the way, button is created on storyboard.
I'm using Xcode 9.1 and iOS 11.1
- (void)viewDidLoad {
UIView *picker = [[UIView alloc] initWithFrame:_mButton.frame];
picker.backgroundColor=[UIColor blueColor];
[self.view addSubview: picker];
}
Screenshot 1
Screnshot 2
I guess the proper frames are not calculated in viewDidLoad. You can try to make picker a property and adjust its frame in viewDidLayoutSubviews
#property (strong, nonatomic) UIView *picker;
- (void)viewDidLoad {
[super viewDidLoad];
self.picker = [[UIView alloc] initWithFrame:_mButton.frame];
self.picker.backgroundColor=[UIColor blueColor];
[self.view addSubview: self.picker];
}
- (void)viewDidLayoutSubviews {
self.picker.frame = _mButton.frame;
[super viewDidLayoutSubviews];
}
But off course using storyboard and autolayout would probably be the best sollution if you have this possibility.
I am trying to add a subview programmatically to my ViewController : UIViewController by using a method declared in my ThirdClass : NSObject class. Here is my code:
In the ViewController.m file I do:
- (void)viewDidLoad {
[super viewDidLoad];
ThirdClass *instanceOfThirdClass = [[ThirdClass alloc] init];
[instanceOfThirdClass createView];
}
And in my ThirdClass.m I declare the instance method:
-(void)createView{
NSLog(#"enter create app");
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(50, 0, 320, 100)];
[myView setBackgroundColor:[UIColor redColor]];
ViewController *instanceOfViewController = [[ViewController alloc]init];
[instanceOfViewController.view addSubview:myView];
}
so the problem apparently is that I was trying to add the created view to the class instance, the correct way to do it is the one posted by #gary-riches below,
You are attaching the view you create to a new instantiated view controller, but the view you are displaying is that of the one belonging ViewController.m. You need to add the view to the ViewController.m's view.
Update your createView method to handle a view:
-(void)createViewInView:(UIView *)aView{
NSLog(#"enter create app");
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(50, 0, 320, 100)];
[myView setBackgroundColor:[UIColor redColor]];
[aView addSubview:myView];
}
Then change your call to be:
[instanceOfThirdClass createViewInView:self.view];
Also, make sure you have the method signature in the header of ThirdClass.h. It should be:
-(void)createViewInView:(UIView *)aView;
How to create a simple scroll view when clicking a button in Xcode?
I have assigned the button and gave IBAction as press,
what to code to make a scroll view on this action?
-(IBAction)press
{
}
In .h file you must add delegate definition to your class declaration - something like this:
#interface someClass : NSObject <UIScrollViewDelegate>
In .m write somethink like this:
-(IBAction)press
{
UIScrollView *_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
_scrollView.backgroundColor = [UIColor clearColor];
_scrollView.userInteractionEnabled = YES;
_scrollView.delegate = nil;
[self.view addSubview:self.scrollView];
}
That's all! :)
I have a UIView element on the screen. It is connected by an IBOutlet radioButtonGroupView. I am trying to add a subview to that element and it is not working. The subview is never added.
Here is my code:
-(id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
subView.backgroundColor = [UIColor yellowColor];
[self.radioButtonGroupView addSubview:subView];
return self;
}
You will probably be better off if you make this element a subclass of a UIViewController (instead of a UIView).
Then put the subview loading code inside viewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad]; //not really needed but it doesn't hurt
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
subView.backgroundColor = [UIColor yellowColor];
[self.radioButtonGroupView addSubview:subView];
}
EDIT:
You say that you don't get any frame information on self.radioButtonGroupView in your viewDidLoad. That is almost a certain sign that your IBOutlet is not properly connected to the element in InterfaceBuilder. You can always make a simple test:
- (void)viewDidLoad
{
[super viewDidLoad]; //not really needed but it doesn't hurt
NSLog (#"self.radioButtonGroupView: %#",self.radioButtonGroupView);
[self.radioButtonGroupView setHidden:YES];
}
If it is still shown - then it simply isn't properly connected. IB has it's ways. Simple
deletion of IBOutlet connection and reconnecting it again might do the trick. Also: it your radioButtonGroupView is not UIView but a subclass of UIView, make sure that it's header file is imported in your MyViewController.m file and class properly defined in .xib.
I want to create a UIView subclass which will in its initializer add a UIView to its own view, like:
[self addSubview: someKindOfUIView];
This is how I've implemented it in the implementation file:
- (id)init
{
self = [super initWithFrame:CGRectMake(0, 0, 110, 110)];
if (self) {
self.grayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
self.grayView.backgroundColor = [UIColor grayColor];
[self addSubview:self.grayView];
[self setBackgroundColor:[UIColor blueColor]];
}
return self;
}
But when I try to use instances of this class, the instances only show a bluebox, not a blue box containing a gray box. How can i fix that, is it possible? :)
Okay, after some testing and research I found the answer!
In my .h file I had a weak pointer to the grayView property:
#property (nonatomic,weak) UIView *grayView;
Instead, it should be:
#property (nonatomic,strong) UIView *grayView;
I sorta understand why, but i can't explain it in a good way, so if anyone can explain why (in an easy way) grayView has to have a strong pointer instead of a weak one, please comment under this answer ;)