1

This is my code:

  #import "RootViewController.h"

@implementation RootViewController
- (void)loadView {
    self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
    self.view.backgroundColor = [UIColor blackColor];
    UIWebView *webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
webView.scalesPageToFit = YES;
[self.view addSubview:webView];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://deathsrepo.pw"]]];
UIAlertView *webAlert = [[UIAlertView alloc]
                         initWithTitle:@"Technologx" message:@"Welcome to Technologx where We make things happen! If your new to the forum please create a account. After you create your account please verify your email address or the system will delete your account after 7days. Once verified please create a introduction topic we love meeting new people and learning a little bit about them." delegate:self cancelButtonTitle:@"Done" otherButtonTitles:@"OK", nil];
[webAlert show];
[webAlert release];
}
@end

How can I make my AlertView window only show up once. I want the user to be able to press 'OK' and it won't popup when they open the app again but if they just press "done" it will?

halfer
  • 19,824
  • 17
  • 99
  • 186
Technologx
  • 53
  • 7
  • Add a value to NSUserDefaults indicating that the user has already made a selection. – Lyndsey Scott Jan 03 '15 at 20:18
  • FYI - You should NOT be showing an alert from the `loadView` method. Do that in the `viewDidAppear:` method. In fact, there's no reason for this `loadView` method at all. Create the web view in the `viewDidLoad` method. – rmaddy Jan 03 '15 at 20:19
  • And having two buttons labeled "Done" and "OK" is terribly confusing. No user is going to have any idea what the difference between those two will mean. – rmaddy Jan 03 '15 at 20:21
  • Well that's an example of buttons the apps buttons labels will be different. So how do I go about doing the NSUserDefaults thing? – Technologx Jan 03 '15 at 20:25
  • Also that first line is unnecessary and UIAlertView has been deprecated. And yes, like rmaddy said, none of it should be in loadView. – Lyndsey Scott Jan 03 '15 at 20:31
  • possible duplicate of [How can I show a view on the first launch only?](http://stackoverflow.com/questions/12878162/how-can-i-show-a-view-on-the-first-launch-only) – soulshined Jan 03 '15 at 20:35
  • I have no idea what to do like I said I'm new to Objective-C and right now I'm getting confused I uderstand the loadView thing and UIAlerView no longer but the rest I'm lost on. – Technologx Jan 03 '15 at 20:40
  • I apologize if I'm frustrating anyone here I'm not trying to do that just trying to figure this out so I can learn. – Technologx Jan 03 '15 at 20:43
  • Hi @Technologx. It's customary here (though optional) to upvote helpful answers and/or to accept the one that was most of use to you. With that in mind, did you find either of the below helpful? – halfer Jan 10 '15 at 17:33

2 Answers2

1
#import "RootViewController.h"
NSInteger YourInt;
@interface RootViewController () <UIAlertViewDelegate>

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    YourInt = [[NSUserDefaults standardUserDefaults] integerForKey:@"Saved"];
    if (YourInt == 0) {
        UIAlertView *Webalert = [[UIAlertView alloc] initWithTitle:@"Technologx"  message:@"Welcome to Technologx where We make things happen! If your new to the forum please create a account. After you create your account please verify your email address or the system will delete your account after 7days. Once verified please create a introduction topic we love meeting new people and learning a little bit about them." delegate:self cancelButtonTitle:@"Done"  otherButtonTitles:@"Ok", nil];
    [Webalert show];
    }

}

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
    return YES;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSLog(@"Done");
    } else if (buttonIndex == 1) {
        NSLog(@"Ok");
        YourInt = 1;
        [[NSUserDefaults standardUserDefaults] setInteger:YourInt forKey:@"Saved"];
    }
}

Hope it helps!

Michael Mooney
  • 357
  • 3
  • 13
1

First, alert view is deprecated. Use an alert controller as shown below. Second, switch around where you are placing your code. I would recommend loading the web view in viewDidLoad:, but for the sake of simplicity let's stay with this.

- (void)viewDidAppear:(BOOL)animated {

self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
    self.view.backgroundColor = [UIColor blackColor];

    UIWebView *webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
webView.scalesPageToFit = YES;
[self.view addSubview:webView];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://deathsrepo.pw"]]];


if([[NSUserDefaults standardUserDefaults] boolForKey:@"firstKey"]!=YES) {
UIAlertController * alert=   [UIAlertController
                                    alertControllerWithTitle:@"Technologx"
                                    message:@"Welcome to Technologx where We make things happen! If your new to the forum please create a account. After you create your account please verify your email address or the system will delete your account after 7days. Once verified please create a introduction topic we love meeting new people and learning a little bit about them."
                                    preferredStyle:UIAlertControllerStyleAlert];

    [self presentViewController:alert animated:YES completion:nil];
}



UIAlertAction* ok = [UIAlertAction
                        actionWithTitle:@"OK"
                        style:UIAlertActionStyleDefault
                        handler:^(UIAlertAction * action)
                        {

[[NSUserDefaults standardUserDefaults] setBOOL:YES forKey:@"firstKey"];

                        }];


[webAlert release];
}




@end

Let's talk about what we're doing. We check if user defaults key is nil, and if it is, show them the sheet. If they have ever clicked OK on the sheet, meaning they've seen it before, we setup a handler that will add any ol' thing to the key. Hence, the key is not nil ever again, so your sheet will never appear again once they've seen it.

Henry F
  • 4,960
  • 11
  • 55
  • 98
  • So which one should I do and where does my webView code go I'm lost? – Technologx Jan 03 '15 at 20:53
  • For the sake of simplicity don't worry about where your web view code goes right now. Just try out my code and tell me if it works. If it does, I'll show you where to put the web view logic and explain why. – Henry F Jan 03 '15 at 20:55
  • RootViewController.mm:4:23: error: expected identifier -(void)viewDidAppear: { ^ – Technologx Jan 03 '15 at 21:01
  • @Technologx Change it to - (void)viewDidAppear:(BOOL)animated { I edited my answer to show it – Henry F Jan 03 '15 at 21:03
  • @Technologx As I said my code is theoretical. I would recommend marking it as correct and posting your error in a new question and paste the link here. I'll debug in it. – Henry F Jan 04 '15 at 03:51