iOS3, iOS4, xibbed UIViewController and displaying data - iphone

I'm writing an app that will run on iOS3.0 and up.
The app has a custom UIViewController (say), which I'm instantiating from a .xib file. It's view comprises a single UILabel, which I've correctly declared and synthesized etc. in my custom UIViewController header and implementation files.
I'd like to set the text of this UILabel dynamically, and for it to be shown to the user by the time my UIViewController appears. For sake of argument please assume the text setting method is expensive.
The catch is on iOS3.0 with my UIViewController at least, -(id)initWithNibName:(NSString *) aNibNameOrNil bundle:(NSBundle *) aNibBundleOrNil returns before -(void)viewDidLoad does, but on my iOS4.0 device it's the other way around. The text label can therefore be nil when I don't want it to be.
Thus, I don't know where I can set the text in such a way as to keep both iOS3.0 and iOS4.0 happy.
Any advice here?

Can you please explain how your label comes out to be nil?
If you have taken an outlet for the label, then in either case, in the - (void)viewDidLoad method it cannot be nil, provided the label outlet is properly connected in the xib file.
If you have not taken the label outlet and doing it by code, then instantiate the label again in - (void)viewDidLoad method and set its text right over there and then add it to the view controller's view.
For more information - read the - (void)viewDidLoad and - (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle method in the UIViewController class reference

Related

Change UIViewController to UIView

I am new at iOS development,
I want to draw a line in my app, i created a simple viewbased application,
I came to know that for drawing a line we require - (void)drawRect:(CGRect)rect method, but this method is available in UIView not in
UIViewCOntroller
How to change my class to UIView ?
Here is my code snippet,
// .h file
#interface Financial_Calc : UIViewController{
UIButton *firstBtn,*secondBtn;
}
#end
//.m file
#implementation Financial_CalculatorViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)drawRect:(CGRect)rect {
NSLog(#"drawRect");
}
#end
Any help will be appreciated
You need to keep the view controller you have, and make a custom view class for it to control.
Choose File > New File…
From iOS, Cocoa Touch, Choose 'Objective-C Class', Click Next
Name your class 'MyView' (or anything you like, the convention is to have view at the end)
Make your class a subclass of UIView (type UIView in the 'subclass of' field)
Click next, choose a location to create the file (anywhere in your project's folder is fine).
Add your custom drawing code to drawRect: in MyView.m
In your view controller's Xib file, select the view.
Choose View > Utilities > Show Identity Inspector
In the 'Custom Class' field, change the class field from 'UIView' to 'MyView'
Build & run - your view controller is now controlling your custom view.
It's also possible to do this without using the user interface Xib, but this should get you going.
FYI: you cannot change UIViewController to UIView. One way to do is, create a new subclass of UIView (lets say called CustomUIView). there do your coding of draw rect. and in your ViewController.m in viewDidLoad method create an object of CustomUIView Class (lets say customUIView) and do this
[self.view addSubView:customUIView];

manually start a view controller using storyboards.

I am trying to manually start a view controller using storyboards.
That is written in code as opposed to wiring it up. I tried:
self initWithNibName:(NSString *) bundle:(NSBundle *)
but I am not sure if they are classed as nibs or what they are classed as in storyboards. Any help would be great thanks!
Sounds like you are looking for this method:
AlertContainerViewController *alertContainerViewController =
[[UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:NULL]
instantiateViewControllerWithIdentifier:#"alertContainer"];
You need to give the ViewController object in the Storyboard a unique identifier and specify the subclass. You do both through the Property Inspector.

Outlets and buttons ignored

My problem is that I cannot access any of the controls in a view defined using interface builder. This is the .h code for the Navigation bar (as an example):
#import <UIKit/UIKit.h>
#interface myController : UIViewController {
IBOutlet UINavigationBar *tTitle;
}
#property (nonatomic,retain) UINavigationBar *tTitle;
#end
The implementation (.m) is:
#import "myController.h"
#implementation myController
#synthesize tTitle;
- (void)dealloc {
[tTitle release];
[super dealloc];
}
- (void)viewDidLoad {
tTitle.topItem.title=#"This is my title";
}
In viewDidLoad tTitle (and my other outlets) are always 0x0. I have omitted the two text fields and the button for brevity.
This exact code works in another view in the app without issue. In IB I right click on the file owner icon and it shows my outlets correctly (and the single button action). Yet at run time - nada. I click the button and no response. The title is still the default title. I cannot set the text fields text property because the fields are all 0x0.
The view is linked to the view controller. As near as I can tell everything is identical between the two views that are doing the same thing. Obviously something is awry, but I can't figure it out. Any help would be appreciated.
Okay. I did the ROI and figured it would be easier to delete and recreate the view (one control at a time) in an attempt to see where it was going awry. The answer is the title bar title set worked from the get-go. I have no idea why the other class didn't work. But discretion is the better part of valor in this situation. The new class is working fine. Thanks for everyone's input.

initWithNibName VS NSBundle's loadNibNamed

I've noticed that there are two different ways to load nib/xib files:
via the UIView's initWithNibName:bundle: method
NSBundle's loadNibNamed:owner:options: method.
Can someone explain the differences between these two and when it is more appropriate to use one over the other and in what circumstances?
For instance, if I'm loading a custom table section header view from a nib file in the tableView:viewForHeaderInSection: method, which one would I use?
Or, if I were loading a custom table view cell from a nib file, which one would I use?
NSBundle’s methods are the generic API to use for unarchiving NIBs. They do the actual work (together with NSNib).
UIViewController’s initWithNibName:bundle: on the other hand is a way to initialize a view controller which (might) load its view from a nib. The method does not itself load the nib but just takes note of the name. The controller loads the nib lazily when the view is requested.
I’m not aware of any nib loading in UIView.
If your header's view controller contains IBOutlets to any fields in the nib file it will be better to load the nib file instead of calling initWithNib.
In the view controller of the header file modify the initiation statement as the following .The default statement is commented out.
By doing so you will be able to access the fields in the nib file using the IBoutlets.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
//self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
// Custom initialization.
if ([[NSBundle mainBundle] loadNibNamed:#"NibFile" owner:self options:nil]) {
}
return self;
}

UIViewController issue

I want to get objects values from a UIViewController.
I am using this
SecondViewController *newObject=[[SecondViewController alloc] init];
But newobject has textview, its value is zero. How can I access the textview?
you have to make the textview as a property to be able to access it outside.
Looks like the initialization process is incorrect:
If you use NIB file for you view then you should use
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
to initialize your controller. If you create your view programmatically, you must override controller's -loadView method. (see UIViewController reference for details)
Edit: Make sure that your textview is connected to some controller's outlet in the nib file.