Change the text of a UILabel in viewDidLoad() programmatically - iphone

How do I change the text of my UILabel in viewDidLoad method of my view controller?.
My code below does not update the UI.
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil
bundle:nibBundleOrNil]) {
displayLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 20, 230, 40)];
[self.view addSubview:displayLabel];
}
return self;
}
//
- (void)viewDidLoad {
[super viewDidLoad];
displayLabel.text = #"abc";
}

You should try adding the subview in viewDidLoad.
self.view is an IBOutlet and it will not be loaded into memory at the point that initWithNibName is called, and will be equal to nil.
viewDidLoad is called after the view controller has loaded its associated views into memory. And as such self.view will point to the view you created in your nib file.

Related

scrollview add other page xib's views

i have two .xib file. First xib name is viewcontroller.xib , it contain scrollview using page control.
second xib name is registration.xib , it contain labels textfield and submit button .
my question is that iwant to add the whole views of registration.xib file into the scrollview of viewcontroller.xib file .
what can i do ???????????
//In viewdidload of viewcontroller.m
regiViewController=[[RegistrationViewController alloc]init];
regiViewController.view11.translatesAutoresizingMaskIntoConstraints=NO;
UIView *containerView=[[UIView alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
containerView.backgroundColor = [UIColor purpleColor];
[containerView addSubview:regiViewController.view11];
[scrollView addSubview:containerView];
In registration.m
- (void)setup {
[[NSBundle mainBundle] loadNibNamed:#"RegistrationViewController" owner:self options:nil];
[view11 addSubview:self.view];
self.view11.translatesAutoresizingMaskIntoConstraints=NO;
}
Personally I would use
[[RegistrationViewController alloc] initWithNibName:#"RegistrationViewController" bundle:nil];
rather than loading the XIB from the [setup] method but that's just me. However, in your current code viewcontroller.m never calls the setup method, so the XIB is never loaded. Try this:
//In viewdidload of viewcontroller.m
regiViewController=[[RegistrationViewController alloc]init];
[regiViewController setup];
UIView *containerView=[[UIView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
containerView.backgroundColor = [UIColor purpleColor];
[containerView addSubview:regiViewController.view11];
[scrollView addSubview:containerView];
Thank you dear for reply me but your code not working ...My setup method is called after this line
regiViewController=[[RegistrationViewController alloc]init];
// see Again viewcontroller.m
regiViewController=[[RegistrationViewController alloc]init];
regiViewController.view11.translatesAutoresizingMaskIntoConstraints=NO;
UIView *containerView=[[UIView alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
containerView.backgroundColor = [UIColor purpleColor];
[containerView addSubview:regiViewController.view11];
[scrollView addSubview:containerView];
//registrationviewcontroller.m
#import "RegistrationViewController.h"
#interface RegistrationViewController ()
#end
#implementation RegistrationViewController
#synthesize view11;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[self setup];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setup {
[[NSBundle mainBundle] loadNibNamed:#"RegistrationViewController" owner:self options:nil];
[view11 addSubview:self.view];
self.view11.translatesAutoresizingMaskIntoConstraints=NO;
}
#end

text not shown on uiview

I have defined a label in my viewController and I also set its #property and synthesized it. I have establish link between between file owner and UIlabel in viewDidLoad. I am trying to display this string link:
- (void)viewDidLoad {
getname = [[UILabel alloc] init];
getname.text = #"hello";
[super viewDidLoad];
}
But the string is not displaying on the label on the UIView. Please tell me what I need to do to display a string on a UIView. The view controller and nib file has the same names.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
But when I added viewcontroller.m file want to display something on view, it does not response to that code. How can I fix this?
Change your viewDidLoad like:
- (void)viewDidLoad
{
getname.text = #"hello";
[super viewDidLoad];
}
There is no need of allocating an IBOutlet.
If you added your label in xib then comment the initialization line.
- (void)viewDidLoad
{
//getname = [[UILabel alloc] init];
getname.text = #"hello";
[super viewDidLoad];
}
If you are using a storyboard...
It sounds like you are using a storyboard, in which case you should remove the init, since the storyboard takes care of that for you:
- (void)viewDidLoad
{
getname.text = #"hello";
[super viewDidLoad];
}
By using init with a storyboard, you are redeclaring the object, so the storyboard can't find it.
If you are not using a storyboard...
If you are not using a storyboard, then you need to add the label to the view:
- (void)viewDidLoad
{
getname = [[UILabel alloc] init];
getname.text = #"hello";
[self.view addSubview:getname];
[super viewDidLoad];
}
The line [self.view addSubview:getname]; adds the UILabel as a subview so that it actually shows up.
Just replace this function this code
- (void)viewDidLoad
{
getname = [[UILabel alloc] init];
getname.text = #"hello";
[self.view addSubview:getname];
[super viewDidLoad];
}
Hope it helps you..
Also if you are using IBOutlet in your .h file, don't allocate memory to it as it automatically allocates memory.
So if you have used IBOutlet then your function should be
- (void)viewDidLoad
{
getname.text = #"hello";
[super viewDidLoad];
}

How do I control the size and layout of child views?

I'm diving into iOS development and I'm trying to figure out how to control the size and layout of multiple child views (in this case, two child views). I posted this question in a different context and my question wasn't getting it answered, so I'm reposting it as a much simpler question. I'm attempting to add to children view and position them at (0, 0) and (100, 100) respectively, but the first view (master) I add ends up filling the whole screen. My code is very simple, what am I doing wrong that is preventing me from controlling the size and position of the two children views?
MySplitViewController.m
#import "MySplitViewController.h"
#import "MasterViewController.h"
#import "DetailViewController.h"
#interface MYSplitViewController (){}
#property (nonatomic, strong) MasterViewController *masterViewController;
#property (nonatomic, strong) DetailViewController *detailViewController;
#end
#implementation MySplitViewController
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
self.masterViewController = [[MasterViewController alloc] initWithNibName:nil bundle:nil];
self.detailViewController = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.frame = CGRectMake(0, 0, 768, 1004);
[self.view addSubview:self.masterViewController.view];
[self.masterViewController viewDidLoad];
[self.view addSubview:self.detailViewController.view];
[self.detailViewController viewDidLoad];
}
#end
MasterViewController.m
#import "MasterViewController.h"
#interface MasterViewController ()
#end
#implementation MasterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, 1004) style:UITableViewStylePlain];
table.dataSource = self;
table.delegate = self;
[self.view addSubview:table];
}
#end
DetailViewController.m
#import "DetailViewController.h"
#interface DetailViewController ()
#end
#implementation DetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIView *uiview = [[UIView alloc] initWithFrame:CGRectMake(100, 0, 668, 1004)];
[self.view addSubview:uiview];
}
#end
Thanks so much in advance for your wisdom!
Try setting the frames for self.masterViewController.view and self.detailViewController.view before adding as subview to self.view.
For eg:-
self.masterViewController.view.frame = CGRectMake(0, 0, 100, 100);
self.detailViewController.view.frame = CGRectMake(100, 100, 200, 200);
And you are not supposed to call viewDidLoad method directly. It gets called automatically once the view is loaded on the screen.
You should create UIView derived class for your MySplitViewController view and implement layoutSubviews method

iOS accessing view's variable from view controller

I'm developing on iOS and I'm building my views programmatically. I noticed that when I try to access variables that have to be changed in my view from the view controller they are null. I'll post both the view and its view controller:
RootViewController.h
#import <UIKit/UIKit.h>
#class RootView;
#interface RootViewController : UIViewController {
RootView *rootView;
}
#property (nonatomic,retain) RootView *rootView;
#end
RootViewController.m
#import "RootViewController.h"
#import "RootView.h"
#implementation RootViewController
#synthesize rootView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[rootView release];
[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.
}
#pragma mark - View lifecycle
- (void)loadView{
RootView *rootV = [[RootView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
rootV.rootViewController = self;
self.view = rootV;
[rootV release];
}
- (void)viewDidLoad{
NSLog(#"TEXT: %#",self.rootView.label.text);
self.rootView.label.text=#"HELLO!";
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[self setRootView:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
RootView.h
#import <UIKit/UIKit.h>
#class RootViewController;
#interface RootView : UIView {
RootViewController *rootViewController;
UILabel *label;
}
#property (nonatomic,assign) RootViewController *rootViewController;
#property (nonatomic,retain) UILabel *label;
#end
RootView.m
#import "RootView.h"
#import "RootViewController.h"
#implementation RootView
#synthesize rootViewController;
#synthesize label;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//Create the label
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100,100, 50)];
//Set the font bold
testLabel.font = [UIFont boldSystemFontOfSize:20.0];
//Set the backgroundcolor of the label to transparent
testLabel.backgroundColor = [UIColor clearColor];
//Set the text alignment of the text label to left
testLabel.textAlignment = UITextAlignmentLeft;
//Set the text color of the text label to black
testLabel.textColor = [UIColor blackColor];
testLabel.text = #"01:30";
self.label = testLabel;
[self addSubview:label];
[testLabel release];
}
return self;
}
- (void)dealloc
{
[label release];
rootViewController = nil;
[super dealloc];
}
#end
I changed the code but it seems not working.....
Ok solved I forgot this line "self.rootView = rootV;"
Your view doesn't find out what its controller is until after its -initRootView method returns, but you're trying to use the controller from within that method.
That said, it would be much better if you followed the usual Cocoa Touch pattern for a view controller creating its view. View controllers are supposed to create their views lazily, which is to say that they defer view creation and initialization until the -loadView method is called. You can override -loadView to create your view, and also override -viewDidLoad to do any setup work that needs to be done after the view is created.
Also, it's generally not advisable for a view to know about its controller. The controller should tell the view what to do, not the other way around. If you need the view to send some information to the controller, you usually provide the controller to the view as the view's delegate. But if you just need the view controller to be able to find some subview, like your label, it's probably a good idea to either provide some accessors in the container view for that (so that the view controller can just say something like self.view.label.text = #"some text";. Another options is to set the subview's tag property to some unique value and have the controller use that to find the subview.
The problem is easy to spot, but requires some work to fix.
Looking at your code, something that I immediately want to suggest is to put all your RootView initialization code the loadView method of your RootViewController. That's where it should be (see here why).
Also, if you absolutely need your RootView to have a reference back at RootViewController, you should probably do that in viewDidLoad. But I wouldn't recommend doing that.
When using the MVC pattern, it is the controller's responsibility to initialize and update views. The line self.rootViewController.rootViewLabel = testLabel; should be removed from RootView's implementation. It's not clear what your intention is there, but if you want the rootViewLabel updated, you should let the controller do that.
To sum it all up:
// RootViewController.m
- (id)initRootViewController{
self = [super init];
if(self){
// other init code here
}
return self;
}
- (void)loadView {
RootView *rootV = [[RootView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
self.view = rootV;
[rootV release];
}
- (void)viewDidLoad {
[super viewDidLoad];
// etc...
}
// etc.
Now, as for RootView, here is what it would look like:
RootView.h
#import <UIKit/UIKit.h>
#interface RootView : UIView {
UILabel *rootViewLabel;
}
// moved from RootViewController
#property (nonatomic, retain) UILabel *rootViewLabel;
#end
RootView.m
#import "RootView.h"
#implementation RootView
#synthesize rootViewLabel;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Whatever initialization code you might have
//Create the label
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100,100, 50)];
//Set the font bold
testLabel.font = [UIFont boldSystemFontOfSize:20.0];
//Set the backgroundcolor of the label to transparent
testLabel.backgroundColor = [UIColor clearColor];
//Set the text alignment of the text label to left
testLabel.textAlignment = UITextAlignmentLeft;
//Set the text color of the text label to black
testLabel.textColor = [UIColor blackColor];
testLabel.text = #"01:30";
self.rootViewLabel = testLabel;
[testLabel release];
// add rootViewLabel as a subview of your this view
[self addSubView:rootViewLabel];
}
return self;
}
- (void)dealloc
{
[rootViewLabel release];
[super dealloc];
}
#end
I hope this gives you an idea on how to structure your view initialization code...
(Disclaimer, I can't test this code now, please point out any errors! Thanks)
Not sure why you're doing it like that, but you could probably make it work if you pass the controller into the view's init:
RootView *rootV = [[RootView alloc] initRootView:self];
view's init:
- (id)initRootView:(UIViewController*)controller
{
self.rootViewController = controller;
self.rootViewController.rootViewLabel = testLabel;

Problem with Landscape and Portrait view in a TabBar Application

I have an TabBar app that I would like to be in landscape and portrait. The issue is when I go to tab 2 make a selection from my table then select tab 1 and rotate the device, then select tab 2 again the content does not know that the device rotated and will not display my custom orientated content correctly. I am trying to write a priovate method that tells the view what orientation it is currently in. IN viewDidLoad I am assuming it is in portrait but in shouldAutoRotate I have it looking in the private method for the correct alignment of the content. Please Help!! Here is my code:
#import "DetailViewController.h"
#import "ScheduleTableViewController.h"
#import "BrightcoveDemoAppDelegate.h"
#import "Constants.h"
#implementation DetailViewController
#synthesize CurrentLevel, CurrentTitle, tableDataSource,logoName,showDescription,showDescriptionInfo,showTime, showTimeInfo, tableBG;
- (void)layoutSubviews {
showLogo.frame = CGRectMake(40, 20, 187, 101);
showDescription.frame = CGRectMake(85, 140, 330, 65);
showTime.frame = CGRectMake(130, 10, 149, 119);
tableBG.frame = CGRectMake(0, 0, 480, 320);
}
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = CurrentTitle;
[showDescription setEditable:NO];
//show the description
showDescription.text = showDescriptionInfo;
showTime.text = showTimeInfo;
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *ImagePath = [Path stringByAppendingPathComponent:logoName];
UIImage *tempImg = [[UIImage alloc] initWithContentsOfFile:ImagePath];
[showLogo setImage:tempImg];
[tempImg release];
[self masterView];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
isLandscape = UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
if(isLandscape = YES){
[self layoutSubviews];
}
}
- (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.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[logoName release];
[showLogo release];
[showDescription release];
[showDescriptionInfo release];
[super dealloc];
}
#end
I believe that you need to pass the -shouldAutorotateToInterfaceOrientation: method to each view controller. You could do something like this in your UITabBarController:
- ( BOOL )shouldAutorotateToInterfaceOrientation:( UIInterfaceOrientation )
interfaceOrientation
{
for ( UIViewController *viewController in [ self viewControllers ])
[ viewController
shouldAutorotateToInterfaceOrientation:interfaceOrientation ];
return YES;
}