Reputation: 13
I'm trying to add a subView on a button click. But when clicking the button, the view does not appear.
Here is the very simply code for the button click:
-(IBAction) dimChangeBtnClick: (id) sender
{
[self addSubview:myHelloWorldViewController.view];
}
myHelloWorldViewController is an instance of the HelloWorldViewController class.
HelloWorldViewController.h:
#import <UIKit/UIKit.h>
@interface HelloWorldViewController : UIViewController
-(IBAction)showMessage;
@end
and HelloWorldViewController.m:
#import <Foundation/Foundation.h>
#import "HelloWorldViewController.h"
@implementation HelloWorldViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
-(IBAction)showMessage
{
UIAlertView *hellowWorldAlert = [[UIAlertView alloc] initWithTitle:@"My First App" message:@"Hello, World!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[hellowWorldAlert show];
}
@end
The view I want to show is HelloWorldViewController.xib
Is there some additional linking required between the objective-c code and the .xib file besides just naming them the same? As might be apparent from my sophomoric problem, I'm very new to iOS so any advice would be greatly appreciated. Please let me know if there is any key information I have left out.
Upvotes: 0
Views: 1297
Reputation: 181
If you want to embed the view from another UIViewController into your main view controller, there is a bit of a process involved to properly transfer some of the control of that view over to the main view controller. This is described in detail in these Apple docs
If you prefer a more tutorial-style walkthrough, then you can try this one
Upvotes: 1
Reputation: 813
Here you are using another ViewController so you must have to first init it and then you have to present it.
SampleViewController *new = [[SampleViewController alloc]initWithNibNam:@"SampleViewController" bundle:nil];
[self presentViewController:new animated:Yes completion:nil];
If you are using simple View in same ViewController you can simply add it in your current view using addsubView function.
Upvotes: 0
Reputation: 536027
This line is problematic:
[self addSubview:myHelloWorldViewController.view];
First, you are referring to a variable called myHelloWorldViewController
. But what is it, and how did it obtain its value? You don't show that, so there is no reason to believe that it even is a HelloWorldViewController.
Second, what you are doing is wrong. You must not create a view controller merely as a way to go dumpster-diving into a nib file and pull out a view. You are free to load a view from a nib, but not this way. And as for putting another another view controller's view inside your view, there are very strict rules about how to do that (you have to formally configure the other view controller as a child of your view controller), and you are not following those rules.
Upvotes: 0
Reputation: 93
Did you have init myHelloWorldViewController?
myHelloWorldViewController = [[HelloWorldViewController alloc] init];
Upvotes: 0