Add subview programmatically using instance method - ios

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;

Related

How to remove a programmatically created subview by pressing a button within the subview

I currently have a barbutton:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(doneDate:)];
It calls the following action:
- (IBAction)doneDate:(id)sender{
[self removeDateView]
}
Which calls the following method:
- (void)removeDateView{
NSLog(#"subviews of view3.view: %#",self.View3.subviews);
[self.View3.subviews. makeObjectsPerformSelector: #selector(removeFromSuperview)];
}
The subview that I'm trying to remove is
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0 + 210)];
At the moment it just deletes everything within that View, I can't seem to remove the view called containerView which has the datepicker and toolbar.
As erhnby stated, you could use a tag - which is a great method, but I always try to shy away from looping through a view's subviews whenever I can. Personally, I would make the view you are going to remove an instance variable, and when you want to remove it you can call remove directly on it... Just made a simple example that does this:
.h file:
#import <UIKit/UIKit.h>
#interface TestViewController : UIViewController {
UIView *_containerView;
}
#end
.m file:
#import "TestViewController.h"
#interface TestViewController ()
#end
#implementation TestViewController
- (id)init {
self = [super init];
// create the bar button and set it as the right bar button on the navigation bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(removeDoneDate)];
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// create the container view and add it as a subview
_containerView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 100, 100)];
_containerView.backgroundColor = [UIColor redColor];
[self.view addSubview:_containerView];
}
- (void)removeDoneDate {
// remove it
[_containerView removeFromSuperview];
}
#end
Results in this to start:
Press button...
(sorry, didn't realize the white on white would be that hard to see)
set tag for that will remove view
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0 + 210)];
[containerView setTag:100];
and find it and removeFromSuperView
for (UIView* view in self.View3.subviews) {
if ([view isKindOfClass:[UIView class]] && view.tag == 100) {
[view removeFromSuperview];
}
}

How to create a scroll view programmatically

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! :)

How can I make this UILabel accessible to other classes?

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.

I want to add a view that will persist through out the application?

I want to add a view that will persist through out the application?
How can i achieve this?
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 430, 320, 50)];
myView.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.5];
[self.window addSubview:myView];
This is not working.
Thanks in advance...
There are several ways in achieving that. It's not clear if you want a view like that be in each view controller, or you want the same instance in each view controller.
Since I don't see any reason to have one shared instance (basing on your description), my approach would be to subclass UIViewController, let's call it SOMyViewController, and then inherit all view controllers in your app from SOMyViewController.
Then, I'd override the 'viewDidLoad' method of SOMyViewController as follows:
- (void) viewDidLoad {
[super viewDidLoad];
if ([self addMyCustomView]) {
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 430, 320, 50)];
myView.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.5];
[self.window addSubview:myView];
}
}
/**
Override this in all your subclasses to decide whether to display the custom view or not
*/
- (BOOL) addMyCustomView {
return YES;
}
If instead you want the same instance shared among view controllers, I would change the above code as follows:
static UIView *mySharedView;
+ (void) initialize {
mySharedView = [[UIView alloc]initWithFrame:CGRectMake(0, 430, 320, 50)];
mySharedView.backgroundColor = [[UIColor redColor]colorWithAlphaComponent:0.5];
}
- (void) viewDidLoad {
[super viewDidLoad];
if ([self addMyCustomView]) {
[self.window addSubview:mySharedView];
}
}
/**
Override this in all your subclasses to decide whether to display the custom view or not
*/
- (BOOL) addMyCustomView {
return YES;
}

How to load view from the UIView Controller? Clarification needed

I know this is very basic, but after massive refactoring, i remain a bit confused.
I have a scene created with the help of a storyboard. When a button on a main menu screen is tapped, a GameView should load.
GameView has a controller GameViewController
When the program runs, the GameViewController is called that in turn
- (void) viewWillAppear:(BOOL)animated {
[self setGameView:[[GameView alloc] initWithFrame
:CGRectMake(0, 0, 300, 480)]];
[self setView:[self gameView]];
[self loadView];
}
What shows on the simulator, however is not a GameView, but a mock up created via the Storyboard.
What am i missing please?
I also tried the following in UIViewController to no avail
UIView *gameView = [[GameView alloc] initWithFrame:CGRectMake(0, 0, 300, 480)];
[[[self view] superview] addSubview:gameView];
Consider
UIView *gameView = [[GameView alloc] initWithFrame:CGRectMake(0, 0, 300, 480)];
[[self view] addSubview:gameView];

Resources