使用自定义AlertView - 窗口层次结构?

时间:2015-06-23 19:10:06

标签: ios objective-c cocoa-touch uistoryboard hierarchy

所以我一直在构建一个使用我在another question中找到的自定义AlertView的应用程序。

完整代码可在Github上找到。

具体而言,可以找到CustomiOSAlertView.m HERE

这就是我创建视图的方式,我调用它来显示附加到UIStory板中UIImage的警告表格IBAction,这里是行动:

- (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];
}

调用它(使用上面附带的CustomiOSAlertView.m):

-(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];
}

创建这样的视图:

- (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;
}

在我创建单独的UIStoryboard之前它工作得很好,并在AppDelegate.m中使用以下代码来为特定设备实例化正确的故事板:

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];
}

现在,当我测试应用程序(在模拟器和设备上)时,它会加载正确的故事板和内容,但不会显示自定义AlertView ...一旦我注释掉上面的代码(实例化)特定故事板),并运行应用程序,自定义AlertViews再次工作......

我猜这与View层次结构有关吗?是否可以通过手动实例化故事板来实现这一点,就像我使用上面的代码一样?

修改

请确保使用Rob的回答,您没有在&#34;主要界面&#34中的下拉标签菜单中选择了故事板; ...将其留空,工作对我来说,现在从AppDelegate实例化一个故事板,并在调用时在UIStoryboard上创建一个AlertView。

Deployment Info - xCode

1 个答案:

答案 0 :(得分:1)

听起来你在实例化故事板之前在AppDelegate 中调用了自定义视图方法。

您应首先实例化故事板,然后从正确的故事板控制器中调用该方法。

例如:

<强>的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"]'

}

编辑:在此处添加了示例代码:https://github.com/robcontreras/CustomAlert