I have in my application a uialertview, that has two buttons, the first one is cancel button , and the second one is ok. when i press on OK button, I want to go to the view controller which has a storyboard identifier "RootView", but it doesn't work.
I put this below code in the appdelegate.m method.
- (void)showAlarm:(NSString *)text {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"show alarm"
message:text delegate:self
cancelButtonTitle:#"cancel"
otherButtonTitles:#"ok", nil];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
NSLog(#"cancel");
}else if (buttonIndex == 1){
NSLog(#"ok");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
UINavigationController *navigationController = [storyboard instantiateViewControllerWithIdentifier:#"navigationController"];
[navigationController presentViewController:[storyboard instantiateViewControllerWithIdentifier:#"RootView"] animated:YES completion:nil];
}
}
Do you wanna push the new view controller ou present it modally? Assuming the last, maybe this helps you:
RootView *viewController = [storyboard instantiateViewControllerWithIdentifier:#"RootView"];
UINavigationController *navigationController = [UINavigationController alloc] initWithRootViewController:viewController];
[self presentViewController:navigationController animated:YES completion:nil];
Please see my sample code it works for me
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[self showAlert];
return YES;
}
-(void)showAlert
{
UIAlertView *alertForCall = [[UIAlertView alloc]initWithTitle:#"" message:#"time to choose" delegate:self cancelButtonTitle:#"YES" otherButtonTitles:#"NO", nil];
[alertForCall show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
self.viewController = [[NewsTestViewController alloc] initWithNibName:#"NewsTestViewController" bundle:nil];
UINavigationController *testNavi = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = testNavi;
[self.window makeKeyAndVisible];
}
}
Related
I want to push to a view controller from AppDelegate through an alert view. But its not working. Only the alert view dismisses. Where is the problem? Thanks in advance for the help. (N.B > my initial view is in the storyboard but i am pushing into a view controller nib)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
loginReapeat = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:#selector(repeatLoginProcess) userInfo:nil repeats:YES];
//First Launch Settings
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"FirstLaunch"])
{
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"FirstLaunch"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self alertShow];
}
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
return YES;
}
-(void)alertShow{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Help!" message:#"Need some help to use this App? Please tap the 'Help' button." delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Help",nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Help"])
{
SignUp *signUp = [[SignUp alloc]initWithNibName:#"SignUp" bundle:nil];
[self.navigationController pushViewController:signUp animated:YES];
}
}
Try this:
SignUp *signUp = [[SignUp alloc]initWithNibName:#"SignUp" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:signUp];
self.window.rootViewController = self.navigationController;
You cannot push from within the App Delegate, instead you can decide which UIViewController to show as a rootviewcontroller depending upon user's selection from the UIAlertView.
self.window.rootViewController = self.navController;
SignUp *signUp = [[SignUp alloc]initWithNibName:#"SignUp" bundle:nil];
UINavigationController *masterNavigationController = [[[UINavigationController alloc] initWithRootViewController:signUp ] autorelease];
self.window.rootViewController = masterNavigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
ViewControllerName *home=[[ViewControllerName alloc] initWithNibName:#"ViewControllerName" bundle:nil];
self.window.rootViewController=home;
navcontroller=[[UINavigationController alloc]initWithRootViewController:home];
self.window.rootViewController = self.navController.view;
[self.window makeKeyAndVisible];
return YES;
}
This code from my AppDelegate from my app, which not use storyBoard, and this code switch view normaly -
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
LN_ShowNoteVC *showVC = [[LN_ShowNoteVC alloc] init];
_iconBage = _iconBage - 1;
showVC.number = _showP;
NSLog(#"showVC.number = %i", _showP);
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_window.rootViewController = showVC;
[_window makeKeyAndVisible];
}
}
Now, i rewrite my App with using StoryBoard..... and this code is not working...i get black window
How i can switch view from AppDelegate???
This is answer)) I Get it) =)
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
LN_ShowNoteVC *showVC = [[LN_ShowNoteVC alloc] init];
_iconBage = _iconBage - 1;
showVC.numberPhoto = _showP;
NSLog(#"showVC.numberPh = %i", _showP);
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"ShowN"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
_window.rootViewController = vc;
[_window makeKeyAndVisible];
}
}
I need to add a tabbarcontroller with ViewControllers when Facebook login is successful.BUt Couldn't understand how?
I have in appDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
SearchView *first=[[SearchView alloc]
initWithNibName:#"SearchView" bundle:nil];
Login *second=[[Login alloc]initWithNibName:#"Login" bundle:nil];
second.title=#"Login";
NSArray *viewArray=[[NSArray alloc] initWithObjects: first,second,nil];
tabBarController=[[UITabBarController alloc] init];
[tabBarController setViewControllers:viewArray animated:NO];
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
[viewArray release];
[first release];
[second release];
return YES;
}
}
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
if (!error) {
FBLogin *fblogin=[[FBLogin alloc]initWithNibName:#"FBLogin" bundle:nil];
[self.window addSubview:fblogin.view];
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[[FBSession activeSession] closeAndClearTokenInformation];
break;
default:
break;
}
[[NSNotificationCenter defaultCenter]
postNotificationName:FBSessionStateChangedNotification
object:session];
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
In FBLogin.m :
-(void)viewDidAppear:(BOOL)animated
{
SearchView *searchViewController=[[SearchView alloc]initWithNibName:#"SearchView" bundle:nil];
UserProfile *userprofile=[[UserProfile alloc]initWithNibName:#"UserProfile" bundle:nil];
userprofile.title=#"My Profile";
LogOut *logout=[[LogOut alloc]initWithNibName:#"LogOut" bundle:nil];
logout.title=#"Sign Out";
tab=[[UITabBarController alloc]init];
tab.viewControllers=[NSArray arrayWithObjects:searchViewController,userprofile,logout, nil];
[self presentModalViewController:tab animated:NO];
}
But I couldnot see the tabbarcontroller added in fBLogin.I can see an empty white view.
Y is it so ?
how can I achieve it ?
just set and initialize the UITabBarController in AppDelegate and when you sign in successfull just call UITabBarController as a rootViewController of window with our custom method For Ex...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *viewController1 = [[[yourViewController1 alloc] initWithNibName:#"yourViewController1" bundle:nil] autorelease];
UINavigationController *navviewController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
UIViewController *viewController2 = [[[yourViewController2 alloc] initWithNibName:#"yourViewController2" bundle:nil] autorelease];
UINavigationController *navviewController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
UIViewController *viewController3 = [[[yourViewController3 alloc] initWithNibName:#"yourViewController3" bundle:nil] autorelease];
UINavigationController *navviewController3=[[UINavigationController alloc]initWithRootViewController:viewController3];
UIViewController *viewController4 = [[[yourViewController4 alloc] initWithNibName:#"yourViewController4" bundle:nil] autorelease];
UINavigationController *navviewController4=[[UINavigationController alloc]initWithRootViewController:viewController4];
UIViewController *viewController5 = [[[yourViewController5 alloc] initWithNibName:#"yourViewController5" bundle:nil] autorelease];
UINavigationController *navviewController5=[[UINavigationController alloc]initWithRootViewController:viewController5];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navviewController1, navviewController2,navviewController3,navviewController4,navviewController5, nil];
SearchView *first=[[SearchView alloc]
initWithNibName:#"SearchView" bundle:nil];
Login *second=[[Login alloc]initWithNibName:#"Login" bundle:nil];
second.title=#"Login";
NSArray *viewArray=[[NSArray alloc] initWithObjects: first,second,nil];
yourTabBarController=[[UITabBarController alloc] init];
[yourTabBarController setViewControllers:viewArray animated:NO];
[self.window addSubview:yourTabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
when you sign in success full just call bellow our custom method..
-(void)loadTabBarFromDelegate
{
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setType:kCATransitionFade];
[animation setDuration:0.5];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:
kCAMediaTimingFunctionEaseInEaseOut]];
[[self.window layer] addAnimation:animation forKey:#"transitionViewAnimation"];
}
and when you want to call this method just create object and call this method like bellow...
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate loadTabBarFromDelegate];
i hope this help you...
Try moving your code from viewDidLoad to viewDidAppear.
i use a tabBarController to show photos, and each kind of photos are show in each tab,
so i use one ViewController.xib , and how to show different content(Navigation Item and ImageView) in each tab ?
and my question is :
where and how to write codes next step, does the codes in -(void)tabBarController or in PhotoController.m ? –
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
UIViewController *photoController1 = [[[PhotoController alloc] initWithNibName:#"PhotoController" bundle:nil] autorelease];
UIViewController *photoController2 = [[[PhotoController alloc] initWithNibName:#"PhotoController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:
,photoController1
,photoController2
,nil];
self.tabBarController.delegate=self;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
switch (tabBarController.selectedIndex)
{
case 4:
//how to write code;
break;
case 5:
//how to write code;
default:
break;
}
}
You can make some prepare method in your PhotoController, and make it public, so you can call it from tabBarController didSelectViewController, so the code may look like this:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
switch (tabBarController.selectedIndex) {
case 4:
//configure options for photoController
[viewController prepareForDisplayWithOptions:options];
break;
case 5:
//configure options for photoController2
[viewController prepareForDisplayWithOptions:options2];
break;
default:
break;
}
}
I have a tab bar based application and I wish to create a terms and conditions page, shown only when the application is launched for the first time.
How do I do this?
You can show terms and conditions in UIAlertView as text or present a view controller modally. Have a Bool which would be set to YES if user selects Accept for terms and conditions and save this using NSUserDefaults. Everytime app is launched check the BOOL.
I have created a sample project using XCode default template tabbed application. And here is code snippet that shows Alert View with terms and conditions until user accepts it.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
if(![[NSUserDefaults standardUserDefaults] valueForKey:#"acceptTermsAndConditionsBool"])
{
UIAlertView* tempAlert = [[UIAlertView alloc] initWithTitle:#"Terms And Conditions" message:#"Please read the terms and conditions below for using the app. We may need the app to send us app usage.. blah blah blah" delegate:self cancelButtonTitle:#"Deny" otherButtonTitles:#"Accept", nil];
[tempAlert show];
[tempAlert release];
}
// Override point for customization after application launch.
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
{
case 0:
{
exit(0);
break;
}
case 1:
{
[[NSUserDefaults standardUserDefaults] setObject:#"YES" forKey:#"acceptTermsAndConditionsBool"];
break;
}
default:
break;
}
}