0

So I've been building an App that uses a custom AlertView which I found in another question.

The full code can be found on Github.

Specifically, the CustomiOSAlertView.m can be found HERE.

This is how I create the view, I call the to display the alert form an IBAction attached to an UIImage in the UIStoryboard, here's the action:

- (IBAction)Button1:(id)sender {
    NSString *name      = @"info";
    NSString *content   = @"info 2";
    NSString *info      = @"more info";
    NSString *imageLink = @"image.png";

    NSString *stringContent = [NSString stringWithFormat:@" %@\n\n %@\n\n %@", name, content, info];
    [self ConstructAlertContent:imageLink :stringContent];
}

Which calls this (using the CustomiOSAlertView.m attached above):

-(void)ConstructAlertContent:(NSString *)imgStr :(NSString *)str{
    NSString *imageLink = imgStr;
    NSString *stringContent = str;

    // Here we need to pass a full frame
    CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];

    // Add some custom content to the alert view with above method createDemoView()
    [alertView setContainerView:[self createDemoView:imageLink : stringContent]];

    // Modify the parameters
    [alertView setButtonTitles:[NSMutableArray arrayWithObjects:@"Done", nil]];
    [alertView setDelegate:self]; //add to .h: <CustomIOSAlertViewDelegate>

    // You may use a Block, rather than a delegate.
    [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
        //NSLog(@"Block: Button at position %d is clicked on alertView %d.", buttonIndex, (int)[alertView tag]);
        [alertView close];
    }];

    //allow alert to move with phone motion
    [alertView setUseMotionEffects:true];

    //set alert alpha to slightly transparent
    [alertView setAlpha:0.94];

    // And launch the dialog
    [alertView show];
}

And creating the view like this:

- (UIView *)createDemoView:(NSString *)imgStr :(NSString *)str
{
    UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 290, 300)];

    //include image
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(70, 10, 150, 180)];
    [imageView setImage:[UIImage imageNamed:imgStr]];
    [demoView addSubview:imageView];

    //... with additional subviews

    return demoView;
}

It was working great until I created individual UIStoryboards, and used the following code in the AppDelegate.m to instantiate the correct storyboard for a specific device:

if (iOSScreenSize.height == 480){ //iPhone 3.5"

    //instantiate storyboard for iPhone4S
    UIStoryboard *iPhone35Storyboard = [UIStoryboard storyboardWithName:@"iPhone35" bundle:nil];
    UIViewController *initialViewcontroller = [iPhone35Storyboard instantiateInitialViewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = initialViewcontroller;
    [self.window makeKeyAndVisible];
}

if (iOSScreenSize.height == 568){ //iPhone 4"

    //instantiate storyboard for iPhone 5S
    UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:@"iPhone4" bundle:nil];
    UIViewController *initialViewcontroller = [iPhone4Storyboard instantiateInitialViewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = initialViewcontroller;
    [self.window makeKeyAndVisible];
}

if (iOSScreenSize.height == 667){ //iPhone 4.7"

    //instantiate storyboard for iPhone 6
    UIStoryboard *iPhone47Storyboard = [UIStoryboard storyboardWithName:@"iPhone47" bundle:nil];
    UIViewController *initialViewcontroller = [iPhone47Storyboard instantiateInitialViewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = initialViewcontroller;
    [self.window makeKeyAndVisible];
}

if (iOSScreenSize.height == 736){ //iPhone 5.5"

    //instantiate storyboard for iPhone 6+
    UIStoryboard *iPhone55Storyboard = [UIStoryboard storyboardWithName:@"iPhone55" bundle:nil];
    UIViewController *initialViewcontroller = [iPhone55Storyboard instantiateInitialViewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = initialViewcontroller;
    [self.window makeKeyAndVisible];
}

Now, when I test the App on (on both simulator and device) it loads the correct storyboard and content but doesn't show the custom AlertView... As soon as I comment out the code above (to instantiate specific storyboard), and run the app, the custom AlertViews work again...

I'm guessing this is something to do with View hierarchy? Is there away to make this appear even by manually instantiating a storyboard as I do with the above code?

Edit

Make sure, using Rob's answer below, you don't have a storyboard selected in the drop-down tab menu in "Main Interface"... Leaving it blank, worked for me and now instantiates a storyboard from the AppDelegate and also creates an AlertView on top of the UIStoryboard when called.

Deployment Info - xCode

Community
  • 1
  • 1
Reanimation
  • 3,151
  • 10
  • 50
  • 88
  • Where is the code where you add the demoView to the view hierarchy? Also, you are duplicating so much code, why not use a switch/if-else statement on the screen size, conditionally pick the storyboard and write all that duplicated code once? – Tim Jun 23 '15 at 19:47
  • I've updated my question to include the link to the .m... (https://github.com/wimagguc/ios-custom-alertview/blob/master/CustomIOSAlertView/CustomIOSAlertView/View/CustomIOSAlertView.m) and I used that code from a youtube tutorial until I get confident with iOS programming. Thanks. – Reanimation Jun 23 '15 at 19:50
  • I meant where are YOU calling the code to display the alert view? You haven't posted the code where you actually display the alert so it is hard to debug why it isn't appearing. – Tim Jun 23 '15 at 19:51
  • @Jeff Just from an IBAction button press; I've updated my questions at the bottom... – Reanimation Jun 23 '15 at 19:56

1 Answers1

1

It sounds to me that you're calling the custom view method inside the AppDelegate before instantiating the storyboard.

You should instantiate the storyboard first and then call the method from within the correct storyboard's controller.

for example:

AppDelegate:

if (iOSScreenSize.height == 667){ //iPhone 4.7"

    //instantiate storyboard for iPhone 6
    UIStoryboard *iPhone47Storyboard = [UIStoryboard storyboardWithName:@"iPhone47" bundle:nil];
    UIViewController *initialViewcontroller = [iPhone47Storyboard instantiateInitialViewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = initialViewcontroller;
    [self.window makeKeyAndVisible];
}

InitialViewController.m

-(void)viewDidAppear(Bool animated){

  [super animated:animated];

  [self createDemoView:"myImage.png" str:"Custom Alert"]'

}

EDIT: Added sample code here: https://github.com/robcontreras/CustomAlert

Rob Contreras
  • 924
  • 6
  • 8
  • How could this be implemented inside an IBAction button? I have the same ViewController for all storyboard... I thought the AppDelegate did it's job before anything inside a ViewController? – Reanimation Jun 23 '15 at 20:00
  • Right, but see, if you're calling the custom view creation method before the storyboard is presented, then the storyboard will be on top in the stack, and not the custom alert which is what you want, what you want is to have a custom alert when someone presses a button, right? then in the initialViewController, you need an IBAction, much like the one you have in you post edit and then make sure that it is linked to the "Touch Up Inside" event in your storyboard's button – Rob Contreras Jun 23 '15 at 20:04
  • That is what I have... The app runs on all devices, boots the app, opens the correct storyboard, then I can scroll through the page and press the button... Unless I am completely misunderstanding you... It's not possible for me to press the button until the storyboard is present... – Reanimation Jun 23 '15 at 20:07
  • If you could cook something up that would be great... If you just create a blank xCode storyboard, with 2 device size UIStoryboard and load the right one in the AppDelegate as I have above, then use the Custom AlertView .h and .m in github and my code above, you will see :( – Reanimation Jun 23 '15 at 20:17
  • Thanks so much for posting that. Your version is working perfectly... I will have to try and see what differs between our projects... It must be the way I instantiate the storyboard, as you said, I think. Thanks for our help. – Reanimation Jun 23 '15 at 20:55
  • Sure, check that the view controller in the storyboard is marked as an initalViewController too, and if you can mark the answer as right, hope it works out – Rob Contreras Jun 23 '15 at 21:01
  • It's still not working in my project for some reason... It's getting late here so I will investigate more tomorrow... Maybe I've looked at it too long. Basically the only difference I can see is I use fixed size with no auto layout or size classes... – Reanimation Jun 23 '15 at 21:09
  • Just an update Rob, I found out why it wasn't working... it's because I had a storyboard select in "Main Interface" drop down menu under "Deployment Info" (see pic adjusted pic in my question)... where yours didn't... I'm guess there was some conflict there. Since leaving it blank, and instantiating UIStoryboards in the AppDelegate like you, it's not working as I was expecting it to; perfectly. So thank you so so much. I really appreciate your time. Have a good day. – Reanimation Jun 23 '15 at 22:03