II wanted to add a UIBarButton to my UINavigationController. I did that with the help of following code,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *rootController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *addInfoButton = [[UIBarButtonItem alloc] initWithTitle:#"Add Info" style:UIBarButtonItemStylePlain target:self action:#selector(addCustomerInfo)];
self.navigationItem.rightBarButtonItem = addInfoButton;
}
-(void) addCustomerInfo
{
AddInfoViewController *addVC = [[AddInfoViewController alloc] initWithNibName:#"AddInfoViewController" bundle:nil];
[addVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:addVC animated:YES];
}
Should I declare "-(void) addCustomerInfo" in .h file? I already tried that but no luck.
The code still throws the exeption,
2012-08-06 04:16:22.200 TableView[5698:f803] -[RootViewController addCustomerInfo]: unrecognized selector sent to instance 0x6c662b0
2012-08-06 04:16:22.202 TableView[5698:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RootViewController addCustomerInfo]: unrecognized selector sent to instance 0x6c662b0'
The code for your class is correct. You need to change your App Delegate
I suggest creating a property in your App Delegate to store your navigation controller -#property (strong, nonatomic) UINavigationController *navController; - Don't forget to synthesize it.
Then, when you create your Navigation controller, set it to your property - self.navController = [[UINavigationController alloc] init];
This will guarantee your NavigationController is properly retained and that it can be accessed correctly by other classes in your application.
Below some sample code that might make it clearer:
First the AppDelegate header file:
//AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navController;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UINavigationController *navController;
#end
And the implementation file:
// AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize navController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.navController = [[UINavigationController alloc] init];
UIViewController *myVC= [[myVC alloc] init];
[self.navController pushViewController:myVC animated:NO];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}
Check out this tutorial:
http://www.innovatelabs.in/2010/03/implementing-uibarbuttonitem/
I'm not so fluent with iOS, but usually when you use a selector, your add : to it #selector(addCustomInfo:)
and then the function would look like this:
-(void) addCustomInfo:(UIBarButtonItem *)myButton {
NSLog(#"YOU CLICKED ME!");
}
Related
I have searched a lot about this topic and I can't get this code work. When I execute it, it only shows my test NSLog but the code to go from one view to another doesn't do anything. Here you have the following code:
//FirstViewController.h
#import <UIKit/UIKit.h>
#import "StationDetailsViewController.h"
#interface FirstViewController : UIViewController{
NSArray *ListaEstaciones;
NSArray *ListaID;
}
#property (nonatomic, retain) NSArray *ListaEstaciones;
#property (nonatomic, retain) NSArray *ListaID;
#end
//FirstViewController.m
#import "FirstViewController.h"
#import "StationDetailsViewController.h"
#implementation FirstViewController
#synthesize ListaEstaciones;
#synthesize ListaID;
//...
- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"Pushing...");
StationDetailsViewController *controller = [[StationDetailsViewController alloc] initWithNibName:#"StationDetailsViewController" bundle:nil];
[[self navigationController] pushViewController:controller animated:YES];
[controller release], controller = nil;
}
#end
I have tried lots of tutorials and my book but I don't know what is wrong. Thanks a lot, guys.
EDIT: Reading your answers I found that the error is on AppDelegate.m where rootViewController is defined.
//AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UIViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
I don't know what to edit here to make this:
[[self navigationController] pushViewController:controller animated:YES];
work properly.
i think the problem is in [self navigationController] .. put a break point on this line of code and probaly you will find its value = nil cuz of that you are not having your detail controller .. you could solve this like that UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:yourMainViewControllerInstance];
This The appDelegate Code :
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *firstViewController = [[[FirstViewController alloc] initWithNibName:#"FBConFirstViewController" bundle:nil] autorelease];
UIViewController *secondViewController = [[[SecondViewController alloc] initWithNibName:#"FBConSecondViewController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:firstViewController]autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav, secondViewController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
I guess there is some problem either with datasource and delegate settings or your navigation controller.
Check this tutorial UITableView Tutorial
This might be helpful to you.
Enjoy Coding :)
I'm developing an iOS 4 with latest SDK and XCode 4.2 (I'm not using ARC).
I'm developing a Navigation Controller programmatically, and I have a question.
This is AppDelegate.h
#import <UIKit/UIKit.h>
#class ViewController;
#class SecondViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController* navController;
ViewController* viewController;
SecondViewController* secondViewController;
}
#property (strong, nonatomic) UIWindow *window;
- (void) showSecondViewController;
#end
And this is AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
#import "SecondViewController.h"
#implementation AppDelegate
#synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
viewController.title = #"Menu";
navController = [[UINavigationController alloc] initWithRootViewController:viewController];
navController.navigationBar.tintColor = [UIColor blackColor];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
- (void) dealloc
{
[_window release];
[viewController release];
[navController release];
[secondViewController release];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
...
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
...
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
...
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
...
}
- (void)applicationWillTerminate:(UIApplication *)application
{
...
}
- (void) showSecondViewController
{
secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.title = #"Second";
[navController pushViewController:secondViewController animated:YES];
}
My question is about the last method, -(void)showSecondViewController;
May I add this line at the end?
[secondViewController release]
I've profiled the application, and I haven't see any memory leaks. But I have to ask it here, because I'm not sure.
You will get a memory leak if you call showSecondViewController method again.
You should release the secondViewController in your showSecondViewController method.
- (void) showSecondViewController
{
secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.title = #"Second";
[navController pushViewController:secondViewController animated:YES];
[secondViewController release]
}
It will automatically be retained by navController when you do pushViewController:secondViewController
In my app delegate interface I have:
#interface pivcalc1AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
IBOutlet UITabBarController *RootController;
}
#property (nonatomic, retain) IBOutlet UIWindow *Window;
#property (nonatomic, retain) IBOutlet UITabBarController *RootController;
in implementation, I have:
#synthesize Window;
#synthesize RootController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
[Window addSubview:RootController.view];
// Override point for customization after application launch.
[self.Window makeKeyAndVisible];
return YES;
}
In my main xib window I have a tab bar controller which is connected to app delegate as rootController.
When I run the program, the window shows but the tab bar view does not get loaded.
Appreciate any help. Thanks.
Are you have a subViewController in RootController in xib? if you're not, It's code following.
UITabBarController should be have a subViewController.
UINavigationController *localNavigationController;
NSMutableArray *localViewControllerArray = [[NSMutableArray alloc] initWithCapacity:2];
SubViewController *subviewController0 = [[SubViewController alloc] init];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:subviewController0];
[localViewControllerArray addObject:localNavigationController];
[subviewController0 release];
[localNavigationController release];
SubViewController *subviewController1 = [[SubViewController alloc] init];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:subviewController1];
[localViewControllerArray addObject:localNavigationController];
[subviewController1 release];
[localNavigationController release];
RootController.viewControllers = localViewControllerArray;
[localViewControllerArray release];
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
When I do an analyze in xcode, I get this memory leak on the navController in my appdelegate.m.
The app runs fine, but I just can't get this warning to go away. Can anyone help? Is this ok?
Really appreciate anyone help.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
// create window and set up view controller
[window addSubview:navController.view];
[window makeKeyAndVisible];
navController.topViewController.title = SHKLocalizedString(#"Quick Lomo Pro");
navController.navigationBar.tintColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];
navController.navigationBar.translucent = NO;
[navController setToolbarHidden:YES];
}
You are never releasing the "UINavigationController" after you call the init.
You should store a pointer to the navigation controller and release it in you appdelegate dealloc method.
#interface AppDelegate : NSObject <UIApplicationDelegate> {
UINavigationController *navController;
}
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
navController = [[UINavigationController alloc] init];
...
}
-(void) dealloc {
[navController release];
[super dealloc];
}
#end
can we use UITab bar controller in view base application thank you
Yes you can.
you can look at the "TheElements" example that apple provides.
you can find it here:
https://developer.apple.com/library/ios/#samplecode/TheElements/Introduction/Intro.html
look at the app delegate.
its very strait forward example.
for you request i tried to make a simple example:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setupPortraitUserInterface];
return YES;
}
- (UINavigationController *)AchievementsControllerWrappingViewController:(NSInteger*)tabIndex{
switch(tabIndex){
case 0:
FirstViewController *theViewController;
theViewController = [[FirstViewController alloc] init];
break;
case 1:
SecondViewController *theViewController;
theViewController = [[SecondViewController alloc] init];
break;
}
UINavigationController *theNavigationController;
theNavigationController = [[UINavigationController alloc] initWithRootViewController:theViewController];
[theViewController release];
return theNavigationController;
}
- (void)setupPortraitUserInterface {
UINavigationController *localNavigationController;
UIWindow *localWindow;
localWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = localWindow;
[localWindow release];
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:2];
for(int i=0;i<2;i++){
localNavigationController = [self AchievementsControllerWrappingViewController:i];
[localViewControllersArray addObject:localNavigationController];
[localNavigationController release];
}
tabBarController.viewControllers = localViewControllersArray;
[localViewControllersArray release];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
i am not next to xCode and i did it with text edit, so please check it when you use it.
shani
Yes we can..
For that you have to create UITabBarViewController and its Object then refrence it with you Application..like :
in your AppDelegate.h
#interface youAppDelegate.h : UIApplicationDelegate {
UIWindow *window;
YourViewController *viewController;
// Declare Your TabBarController Here
UITabBarController *tabBar;
}
#property (nonautomic, retain) IBOutlet UIWindow *window;
#property (nonautomic, retain) IBOutlet TabBarViewController *tabBar;
#end
in you implementation file's ApplicationDidFinish Launching add the following Code
viewController = [[YourViewController alloc] init];
[viewController addSubView:tabBar];
[self.window addSubView:viewController];
In interface builder you have to add the TabBarController to your MainWindow and relate it with the IBOutLet. Give what ever view you want to display in tabBar.
enjoy..