"initWithNibName" method not being called - cocoa-touch

NSString *nibName = #"SampleViewController"
UIViewController *controller = [[UIViewController alloc] initWithNibName:nibName bundle:nil];
using the above code to move to the "SampleViewController" xib... its working fine but initWithNibName method not triggering in the SampleViewController.
anyone can help me to trigger lifecycle method in SampleViewController

UIViewController is parent from which SampleViewController is implemented. So instead of UIViewController you need to use SampleViewController class instance.
SampleViewController *controller = [[SampleViewController alloc] initWithNibName:#"SampleViewController" bundle:[NSBundle mainBundle]];

Related

calling custom delegates not working

I have made a custom delegate:
Example.h:
#protocol DismissExamplePopoverDelegate
- (void) dismissExamplePopover;
- (int) getUserID;
#end
#interface Example : UIViewController{
id<DismissExamplePopoverDelegate> delegate;
}
#property (nonatomic, assign) id<DismissExamplePopoverDelegate> delegate;
It is called in Example.m like follows:
[[self delegate] getUserID];
In my maincontroller.h:
#import "Example.h"
#interface MainScreen : UIViewController<DismissExamplePopoverDelegate>
maincontroller.m:
-(int) getUserID
{
return 100;
}
the view Example is called by the following method:
ExampleController = [[Example alloc] initWithNibName:#"Example" bundle:nil];
ExamplePopoverController = [[UIPopoverController alloc] initWithContentViewController:ExampleController];
[ExampleController setDelegate:self];
ExamplePopoverController.popoverContentSize = CGSizeMake(600, 480);
if ([ExamplePopoverController isPopoverVisible]) {
[ExamplePopoverController dismissPopoverAnimated:YES];
} else {
CGRect popRect = CGRectMake((self.EditExampleSelectAddButtonProperty.frame.origin.x),
(self.EditExampleSelectAddButtonProperty.frame.origin.y),
(self.ExampleSelectAddButtonProperty.frame.size.width),
(self.ExampleSelectAddButtonProperty.frame.size.height));
[ExamplePopoverController presentPopoverFromRect:popRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
If I place [[self delegate] getUserID] in any function other than viewdidload it works perfectly: returns 100; in viewdidload it returns 0.
What i want to achieve is a delegate to be called automatically when the popover loads. Is viewdidload the best place for it, or is there somewhere else
A couple of people have said this, but haven't been clear enough about the solution.
Instead of doing this:
ExampleController = [[Example alloc] initWithNibName:#"Example" bundle:nil];
ExamplePopoverController = [[UIPopoverController alloc] initWithContentViewController:ExampleController];
[ExampleController setDelegate:self];
Do this:
ExampleController = [[Example alloc] initWithNibName:#"Example" bundle:nil];
[ExampleController setDelegate:self];
ExamplePopoverController = [[UIPopoverController alloc] initWithContentViewController:ExampleController];
What's happening is that -[UIPopoverController initWithContentViewController:] uses its contentViewController's view, causing -[Example viewDidLoad] to be called. Since you haven't yet set the delegate at that point, the delegate is nil, and attempting to call any method on nil returns nil, 0, or 0.0.
Make sure you're actually assigning the delegate property to MainController. For instance, wherever you've initialized Example, you need to set the delegate property:
Example *example = [[Example alloc] init];
example.delegate = self; // Use this if you're initializing Example in MainController
If you're not initializing Example in MainController, instead of "self" use the MainController instance.
In Example.m you need to add line:
[self setDelegate:delegate];
Also, you need to #synthesize delegate;
Hope it help)

Add a UITabBarController to a UIViewController (Xcode 4.6)

I am studying Objective-C and I started to create an App while I am learning it.
I have created a "Single View Application" with two "View Controllers" called "ViewController" and "secondViewController", after that I thought to add an "UITabBarController", so following a tutorial I used the following code in the AppDelegate.m
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self customizeInterface];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UITabBarController *tabController = [[UITabBarController alloc] init];
UIViewController *viewController1 = [[UIViewController alloc] init];
UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:#"Artists" image:[UIImage imageNamed:#"artist-tab.png"] tag:1];
[viewController1 setTabBarItem:tab1];
UIViewController *viewController2 = [[UIViewController alloc] init];
UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:#"Music" image:[UIImage imageNamed:#"music-tab.png"] tag:2];
[viewController2 setTabBarItem:tab2];
tabController.viewControllers = [NSArray arrayWithObjects:viewController1,
viewController2, nil];
self.window.rootViewController = tabController;
[self.window makeKeyAndVisible];
// Override point for customization after application launch.
return YES;
}
Here is the ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
If I run the application, the tab bar is working but I have a blank screen and not the ViewController I have created.
Just because I am a noob, could you please tell me how to link or show the view controllers respectively in each tab?
Please be so kind to say everything also the obvious things.
Thank you in advance
Since you are beginner I would highly suggest you to use storyboards, that will help to manage the interface much more easier because you will not have to code almost anything.
http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1
I am just guessing but your problem might be that you directly init your view controller , you should call initWithNibName: method in your didFinishLaunchingWithOptions. The initWithNibName: is something that you call when you create a controller instance from a nib file.
So basically initWithNibName: is called when the NIB is loaded and instantiated.
I dont know your ViewContoller class names but you should change your view controllers init methods
in your app delegate.m
#import "ViewController.h"
#import "secondViewController.h"
UIViewController *viewController1 = [[ViewController alloc] initWithNibName:#"ViewController" bundle:[NSBundle mainBundle]]; // make sure on interface builder hat you Nib name is ViewController
UIViewController *viewController2 = [[secondViewController alloc] initWithNibName:#"secondViewController" bundle:[NSBundle mainBundle]];// make sure on interface builder hat you Nib name is secondViewController
and please use Upper Case Chars and detailed names for class name secondViewController is not good programming practice
The tabbar controller shows the views, you created in your code. That's the expected behaviour, since you are creating completely new views.
If you created "ViewController" and "secondViewController" in Interface Builder, then you'll have to load the nibs - instead of creating new views - and tell the tabbar controller to use these.
Basically you'll have to change your lines:
UIViewController *viewController1 = [[UIViewController alloc] init];
to something like
UIViewController *viewController1 = [[UIViewController alloc] initWithNibName:#"view1" bundle:nil];
Please see initWithNibName:bundle: for the details.

IOS - Load unknown type of UIViewController

I'm trying to load UIViewController, but not knowing the type until runtime.
So I have some different UIViewControllers, each one with it's own .xib, and with the
following code, the .xib is showed correctly, but the UIViewController is never created, because
it's a child of UIViewController, not an exactly UIViewController:
UIViewController *vc = [[UIViewController alloc] initWithNibName:[[self component] xibName] bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
It works fine for example in this particular case, but obviously not for the rest:
Controller1 *vc = [[Controller1 alloc] initWithNibName:[[self component] xibName] bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
How can I solve it?
Thanks!
you can put your controller classe name in an NSSTring and create the object like this
UIViewController *vc=[[NSClassFromString(ControllerclassName) alloc] init];
[self.navigationController pushViewController:vc animated:YES];

How to use custom UINavigationBar

I have subclass of UINavigationBar.
#interface MyNavigationBar : UINavigationBar
Made some changes and now want that my application NavigationController would use it:
_navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[_window addSubview:[_navigationController view]];
[self.window makeKeyAndVisible];
I want that _navigationController would have MyNavigationBar
How this could be done ?
Thanks.
You have to create a xib with a UINavaigationController in it. You can then select the navigationBar in Interface Builder and change the class to your subclass of UINavigationBar.
Then to make this a little easier to instantiate I add a category to `UINavigationController like:
#interface UINavigationController (DSCNavigationController)
+ (UINavigationController *)dsc_navigationControllerWithRootViewController:(UIViewController *)rootViewController;
#end
#implementation UINavigationController (DSCNavigationController)
+ (UINavigationController *)dsc_navigationControllerWithRootViewController:(UIViewController *)rootViewController;
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"DSCNavigationController" owner:nil options:nil];
NSAssert(1 == [topLevelObjects count], #"DSCNavigationController should have one top level object");
UINavigationController *navigationController = [topLevelObjects objectAtIndex:0];
NSAssert([navigationController isKindOfClass:[UINavigationController class]], #"Should have a UINavigationController");
[navigationController pushViewController:rootViewController animated:NO];
return navigationController;
}
#end
At the top of the class that uses it makes sure to import the category in my case it looks like
#import "UINavigationController+DSCNavigationController"
Then using it looks something like
MyViewController *myViewController = [[MyViewController alloc] init];
UINavigationController *navigationController = [UINavigationController dsc_navigationControllerWithRootViewController:myViewController];
UINavigationController has a read-only property
#property(nonatomic, readonly) UINavigationBar *navigationBar
since it is read-only you have to subclass UINavigationBar and override this property or make it read-write.E.g. :
MyNaviagtionBar *myBar = [[MyNavigationBar alloc] init];
_navigationController.navigationBar = mybar;
Or subclassing:
MyNavigationController.h
#class MyNavigationBar;
#interface MyNavigationController : UINavigationController
#property(nonatomic, strong) MyNavigationBar *navigationBar;
#end
MyNavigationController.m
#implementation MyNavigationController
#synthesize navigationBar = _navigationBar;
#end
And then change
_navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
to
_navigationController = [[MyNavigationController alloc] initWithRootViewController:self.viewController];

Navigate from a view to another

The main: xxxVieController
and the second: OptionViewController
in xxxView controller I have
.h
#interface iMetroAlertViewController : UIViewController {
IBOutlet UIButton *option;
}
-(IBAction) goToOption;
and
.m
#implementation iMetroAlertViewController
-(IBAction) goToOption{
NSLog(#"Works");
OptionViewController *dvController = [[OptionViewController alloc] initWithNibName:#"OptionView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
Of course a OptionViewController.xib, .m and .h are created...
When I push on the button, nothing happens (the link is done cause I have an NSLog
I don't understand why it doesn't work
It probably doesn't work because you don't have a navigation controller, so calling self.navigationController returns nil.
First you have to create the navigation controller
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
RootViewController *mainMenuVC = [[RootViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:mainMenuVC];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}