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;
}
}
Related
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];
}
}
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;
}
I choose the project template as "Single View" .I am trying to push a detail view(UIViewController) on the button click.Button action is firing properly and executing the code [self navigatingController] push......My code is As:
MapDetailViewController *mapDetailViewController = [[MapDetailViewController alloc] init];
[self.navigationController pushViewController:mapDetailViewController animated:YES];
NSLog(#"self.navigation is as %#",self.navigationController);
self.navigationController is null.So i tried to alloc init a local variable of type Navigation Controller and also the variable was able to be non-null(some hex value), still i was unable to push the mapDetailView.
what i am thinking is that i have choosen the wrong template(view based).Should i choose the Navigation-based(Master-detail).Well the below auto generated code is of application delegate:
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
Should some changes be made here for navigation controller?
Any Suggestion?
Your problem is that you don't even have a navigation controller to do the pushing. Change your code to this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
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'm developing an iPhone and iPad application with Xcode 4.2 and latest SDK.
I have created a Tabbed Application without using ARC and I've found this on AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1, *viewController2;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController_iPhone" bundle:nil] autorelease];
viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController_iPhone" bundle:nil] autorelease];
} else {
viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController_iPad" bundle:nil] autorelease];
viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController_iPad" 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;
}
May I need to release viewController1, and viewController2?
Sending autorelease will cause the release message be sent later. So there is nothing to worry about, they will be released.
No. They will be sent autorelease, so they won't need to be released by you coding it in.
Sending autorelease just add them to the current NSAutoreleasePool which is drained at the end of each runLoop. So no need to additionally release them using release.
Tip: if it's a very large object (or many objects for example created in a loop) you really want to release immediately to dealloc it from memory, call release for immediate effect and reducing memory footprint.