I am new to iPhone Developer.
i want to do navigate to new page with animation but i am unable to navigate to new page,
Here is my code snippet,
UIButton *Planbtn = (UIButton *)[self.view viewWithTag:1];
[Planbtn addTarget:self action:#selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
..
-(void)btnClicked{
NSLog(#"btnClicked");
PlanPage *yourViewController = [[PlanPage alloc] initWithNibName:#"PlanPage" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:yourViewController animated:TRUE];
[yourViewController release];
}
i am able to see btnClicked in my Log.
Thanks in Advance !
Write these properties in AppDelegate.h file
#property (strong, nonatomic) YourFirstViewController *yourFirstController;
#property (nonatomic, retain) UINavigationController *navigationControl;
Write this code in AppDelegate.m file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.yourFirstViewController = [[YourFirstViewController alloc] initWithNibName:#"YourFirstViewController" bundle:nil]];
navigationController = [[UINavigationController alloc] initWithRootViewController:self.yourFirstViewController];
[self.window addSubview:[navigationController view]];
[self.window makeKeyAndVisible];
return YES;
}
I think it will be helpful to you.
Being your app view based, you can't push the controller, the simplest thing to do is:
PlanPage *yourViewController = [[PlanPage alloc] initWithNibName:#"PlanPage" bundle:[NSBundle mainBundle]];
[self.view addSubView:yourViewController.view];
[yourViewController release];
If you want to animate it, I'm afraid you have either to implement the animation by yourself, or to add a navigation controller to do the job for you
Related
I have created a new project "Empty Application" template in Xcode 4.3, it is having only two classes AppDelegate.h & .m
I checked with ARC to use automatic reference count while creating the app.
I added two new files "RootViewController" & "NewProjectViewControllers".
I implemented code to set navigation controller as follows in AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
rootViewController = [[MainViewController alloc] init];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[self.window addSubview:navigation.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
and in hte home view (Root view controller) implemented as follows
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Projects";
UINavigationBar *navigationBar = [self.navigationController navigationBar];
[navigationBar setTintColor: [UIColor colorWithRed:10/255.0f green:21/255.0f blue:51/255.0f alpha:1.0f]];
//To set the customised bar item
UIButton *rightBarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[rightBarBtn setBackgroundImage:[UIImage imageNamed:#"plus_new.png"] forState:UIControlStateNormal];
rightBarBtn.frame=CGRectMake(0.0, 100.0, 30.0, 30.0);
[rightBarBtn addTarget:self action:#selector(addProject) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarBtn];
self.navigationItem.rightBarButtonItem = rightBarItem;
// Do any additional setup after loading the view from its nib.
}
- (void) addProject
{
NewProjViewController *editProject = [[NewProjViewController alloc] init];
[self.navigationController pushViewController:editProject animated:YES];
NSLog(#"xxxxxxxxxxxxxxx");
}
But since i used ARC the navigation may dealoc immediately and it doesn't work, All the actions in method works except push to the next view
if i do same thing with out ARC it works fine
How to resolve this issue..? Thanks in advance
In Your appdelegate appdidfinishlaunching method, you have not set
self.window.rootviewcontroller to navigationController. In fact you did not set any rootViewController to Window. Thats why it is not being shown. Please set it before you start to see your ViewController on the screen.
The UIWindow rootViewController property is new with iOS4.
The older technique was to use addSubview.
The new, recommended technique is to set rootViewController.
Try this:
NewProjViewController *editProject = [[NewProjViewController alloc]initWithNibName:#"NewProjViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:editProject animated:YES];
[editProject release];
Have an AppDelegate and a MainViewController.
In MainViewController the UIButton was created, AppDelegate sets a background image and should show it on MainViewController. That's pretty much it but I can't see the UIButton.
Just for alignment purpose I created an IBOutlet and connected the button in IB.
MainViewController.h
#property (strong, nonatomic) IBOutlet UIButton *searchBtn;
MainViewController.m
#synthesize searchBtn;
AppDelegate.m
#synthesize mainVC;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ViewController > setup:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.mainVC = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.window.rootViewController = self.mainVC;
self.window.backgroundColor = [UIColor whiteColor];
// Buttons > Set background images:
[self.mainVC.searchBtn setImage:[UIImage imageNamed:#"search.png"] forState:UIControlStateNormal];
[self.mainVC.view addSubview:self.mainVC.searchBtn];
[self.window makeKeyAndVisible];
return YES;
}
First you don't need IBOutlet if you are creating it programmatically.
Second you probably want to move the creation of the button in viewDidLoad of your view controller rather than in your AppDelegate. It's your view controller's business.
Third you should probably alloc init your button if you create it programmatically:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(...)];
[button setBackgroundImage:someImage];
[button addTarget:self action:#selector(something:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
something like that.
You should be adding buttons in your MainViewController class, not the AppDelegate first off.
You should also set the background of the view in MainViewController, not the AppDelegate.
It should look like this:
AppDelegate.m
#synthesize mainVC;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ViewController > setup:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.mainVC = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.window.rootViewController = self.mainVC;
[self.window makeKeyAndVisible];
return YES;
}
In your MainViewController.m's viewDidLoad put this
- (void)viewDidLoad {
[super viewDidLoad];
// Buttons > Set background images:
[searchBtn setBackgroundImage:[UIImage imageNamed:#"search.png"] forState:UIControlStateNormal];
//If you adding the button in Interface Builder you don't need to add it again
//[self.view addSubview:self.mainVC.searchBtn];
}
I have added navigation controller into my appication in didFinishLaunchingWithOptions like this
LoginViewController *mainView = [[[LoginViewController alloc]initWithNibName:#"LoginViewController" bundle:nil] autorelease];
navigationController = [[[UINavigationController alloc]initWithRootViewController:mainView]autorelease];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
And in my LoginViewController's viewDidLoad i have,
self.navigationItem.hidesBackButton = YES;
[super viewDidLoad];
UIImage *img = [[UIImage alloc] init];
img = [UIImage imageNamed:#"top_bar.png"];
bar = [self.navigationController navigationBar];
[bar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
UIImage *signIn = [UIImage imageNamed:#"signin_btn.png"];
UIButton *phButton = [UIButton buttonWithType:UIButtonTypeCustom];
[phButton setImage:signIn forState:UIControlStateNormal];
phButton.frame = CGRectMake(0.0, 0.0, signIn.size.width, signIn.size.height);
UIBarButtonItem *phBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:phButton];
self.navigationItem.rightBarButtonItem = phBarButtonItem;
[phButton addTarget:self action:#selector(checkConnection) forControlEvents:UIControlEventTouchUpInside];
[phButton release];
When i run the app in my phone it crashes. When i remove the navigationcontroller from the appDelagate it works... Y cant i get my navigation to work properly and how can i avaoid it from getting crashed.
Modify your code
try this code
Declare appdelete.h file
#class LoginViewController;
LoginViewController *viewController;
#property (nonatomic, retain) IBOutlet LoginViewController *viewController;
appdelegate.m file declare
#synthesize viewController;
in didFinishLaunchingWithOptions
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:viewController];
[window addSubview:nav.view];
[window makeKeyAndVisible];
return YES;
-(void) dealloc
{
[viewController release];
......//some code
}
I this this cause crash.
[phButton release];
phButton does not need to be released;
By the way,
phBarButtonItem need to be released;
Try this one...
self.window.rootViewController = navigationController;
instead of [self.window addSubview:navigationController.view];
[phButton release]; remove that line &
img & phBarButtonItem needs to be released
I think the problem is with your autorelease with navigation controller.
you try removing autorelease or
navigationController = [[UINavigationController
alloc]initWithRootViewController:mainView];
try using property (if you have),
self.navigationController = [[[UINavigationController
alloc]initWithRootViewController:mainView]autorelease];
How can I navigate from one page to another page in xcode? remember without using the interface builder... I want the answer programmatically?
Pls be more precise if you want to get what you want.
If you are using view controllers within navigationcontroller you can make use of its pushViewController: or presentModalViewController:. Or if you just want to show another view you can just add the next view to existing view as subview.
Although your question is not much clear but still I would like to give a try...
You can use
UIViewController *yourViewController = [[YourViewControllerClass alloc] initWithNibName:#"<name of xib>" bundle:nil];
[self presentModalViewController:yourViewController animated:YES];
[yourViewController release];
In case the new view is also to be created programmatically, you can do that in the viewDidLoad method of YourViewControllerClass and change the initialization to
UIViewController *yourViewController = [[YourViewControllerClass alloc] init];
In YourViewController when you wish to come back to previous view on some button action you can use
[self dismissModalViewControllerAnimated:YES];
Another way that you can do is
UIViewController *yourViewController = [[YourViewControllerClass alloc] init];
[self addSubview:[yourViewController view]];
and to remove the view you can use
[self.view removeFromSuperview];
Hope this works for you, if yes please communicate....:)
//Appdelegate.m
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
return YES;
}
//In viewcontroller1.M
- (IBAction)GoToNext:(id)sender
{
ViewController2 *vc2 = [[ViewController2 alloc] init];
[self.navigationController pushViewController:vc2 animated:YES];
}
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..