SCLAlertView库 - 如何从另一个视图控制器触发它?

时间:2015-04-08 19:18:49

标签: ios objective-c

我正在使用此警报库SCLAlertView。我的应用程序我有一个左菜单。当用户离线并且他们试图点击菜单项时,我想触发并发出警报。

但由于菜单控制器位于层次结构中的主控制器下方,因此警报显示在主视图下方。有没有办法将它发送到层次结构的顶部,以便它显示在主控制器的顶部?

Menu.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (![Utilities checkConnection]) {
        // hides the menu
        [self.slidingViewController resetTopView]; 

        // Display alert
        SCLAlertView *alert = [[SCLAlertView alloc] init];
        LNViewController *ln = [[LNViewController alloc] init]; // main view controller

        [alert showError:ln title:@"Connection Error"
                subTitle:@"Looks like you lost connection. Please connect to the internet and refresh the view."
        closeButtonTitle:@"OK" duration:0.0f];
    } else {            
        Dep *dep = [self.menuItems objectAtIndex:indexPath.row];
        [self.delegate menuViewControllerDelegate:self selectDep:dep];

        // Deselect the row
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}

1 个答案:

答案 0 :(得分:2)

如果MenuViewController的父节点是MainViewController,则不要将self(MenuViewController)传递给showError方法,而是传递self.parentViewController而不是self。这应该会使警报显示在MainViewController上。

将显示错误的代码更改为以下内容:

[alert showError:self.parentViewController title:@"Connection Error"
        subTitle:@"Looks like you lost connection. Please connect to the internet and refresh the view."
closeButtonTitle:@"OK" duration:0.0f];

初始化MainViewController对象不会自动链接或"指向"到视图控制器层次结构中的活动MainViewController对象。