Customising UITabBarItems in their controllers? - iphone

In previous applications I have customised my tabBarItems by overriding init (see below)
- (id)init {
self = [super init];
if(self) {
UITabBarItem *tabBarItem = [self tabBarItem];
[tabBarItem setTitle:#"ONE"];
}
return self;
}
After looking at the Xcode templates I am now thinking that I would be better to add this customisation to initWithNibName:bundle: instead.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
UITabBarItem *tabBarItem = [self tabBarItem];
[tabBarItem setTitle:#"ONE"];
}
return self;
}
does this make sense, it seems like it does to me, but I just wanted to check?
Gary

It depends on whether you load your controller from a Nib (xib) file or not (and so you do all the work programmatically in the init) I guess

Related

viewDidLoad not Called for UIViewController with UIScrollView and UIPageControl

I am trying to add a page with a UIScrollView and UIPageControl to my app. I am making the call to the new class from a UITableView as below.
MultiPageViewController *mpvc = [[MultiPageViewController alloc]initWithNibName:#"MultiPageViewController" bundle:[NSBundle mainBundle]];
[[self navigationController]pushViewController:mpvc animated:YES];
The viewDidLoad function is never called, thus my UIScrollview and UIPageControl are never initialized. When I go along further and call viewWillAppear these objects are both still nil. Further the view never appears only a white screen. Below is the code from initWithNibName and viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
NSLog(#"initWithNibName returned self != null");
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"View did load called");
}
Thanks for your help in advance.

How to create viewController in PhoneGap to work with main view

I want to open file with [UIDocumentInteractionController presentPreviewAnimated];
I've created view controller
#interface FileViewController : UIViewController <UIDocumentInteractionControllerDelegate>
#end
#import "FileViewController.h"
#interface FileViewController ()
#end
#implementation FileViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (UIViewController *) documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
And trying to preview the file
UIDocumentInteractionController *controller = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
vController = [[FileViewController alloc] init];
controller.delegate = vController;
[controller presentPreviewAnimated:YES];
And get error:
Warning: Attempt to present <QLPreviewController: 0x1e56e0a0> on <FileViewController: 0x1ec3e000> whose view is not in the window hierarchy!
I may set root view controller
[[[[UIApplication sharedApplication] delegate] window] setRootViewController:vController];
But I dont want to use new view. I need to use the PhoneGap's main view. How can I do this?
The problem is solved
CDVViewController* mainController = (CDVViewController*)[ super viewController ];
[mainController addChildViewController:vController];

why -(id)init not see my objects;

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
preview.text = "hello";
}
return self;
}
Preview it's a simple UILabel;
Just because you created your view controller from a nib doesn't mean it immediately loads it.
You need to put any code that requires ui components to be created in viewDidLoad i.e.
- (void)viewDidLoad {
preview.text = "hello";
}
If you check, preview will be nil inside your init method so setting text on it won't work!
because your iboutlets (the UI components) are loaded in the viewdidload method
you can't acces them in the init

Navigating to another view using the UINavigationController

I'm using the UINavigationController in my app and for some reason it won't allow me to switch pages to the third xib called CompanyView. It switches fine from first to second but not second third. I'm probably doing something wrong but if someone could look over my code that would be great. I've got the button set correctly I believe.
Here is my .h file for the xib that won't change view:
#import <UIKit/UIKit.h>
#import "CompanyView.h"
#interface MenuView : UIViewController
-(IBAction)btnClicked:(id)sender;
#end
Here is my code for the .m file:
#import "MenuView.h"
#implementation MenuView
-(IBAction)btnClicked:(id)sender {
CompanyView * companyView = [[CompanyView alloc] init];
companyView.title = #"Company";
[self.navigationController pushViewController:companyView animated:YES];
[companyView release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (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)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Try initializing your Company View this way.
CompanyView *companyView = [[CompanyView alloc] initWithNibName:#"CompanyView" bundle:nil];
this is how i set it up:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
CategoryPresetViewController *controller = [[CategoryPresetViewController alloc] initWithPVCDelegate:avc];
controller.title = [[factoryCategoryNameArray objectAtIndex:indexPath.row] retain];
if (controller != nil){
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
maybe it can help you

Extending UIVIewController of my own I got error accessing self.view

I have created a View controller with some methods I need to use often in different aplications. It works like a charm if I use it directly but when I try to create another UIViewController that extend my class I cannot access self.view anymore. This is the init method of the original class:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil slideFrom:(HalfViewControllerType) from {
self.slideFrom = from;
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
CGRect viewScreen = [self.view bounds];
[self moveTo:startPoint inDuration:0.0];
}
return self;
}
At the point of retrieving [self.view bounds] I got an EXC_BAD_ACCESS.
Even hacking the values manually it then fail to all the other self.view like transform animation and so on.
The call to create the view is this, but it never got after the init method:
SelectViewController *sVC = [[SelectViewController alloc] initWithNibName:#"SelectViewController" bundle:nil slideFrom:top];
[sVC setDelegate:self];
[self.view addSubview:sVC.view];
[sVC slideIn];
[sVC release];
Any help on understanding what I am doing wrong would be really appreciated.
Francesco.
I couldn't understand what was happening, so I re-wrote everything and now works. I can just guess I was releasing something I shouldn't have.