【问题标题】:How to force user interaction with UIView before continuing?如何在继续之前强制用户与 UIView 交互?
【发布时间】:2013-10-23 09:37:14
【问题描述】:

在 StackOverflow 上许多其他人的帮助下,我终于能够在用户点击某个人脸或某个人时显示个人资料屏幕弹出 UIView。

但是,在我的应用程序(iOS 6、Xcode 4.6)中,我还需要弹出视图将索引返回给调用 ViewController,因为下一步是使用该索引刷新屏幕。例如,用户单击主 ViewController 中的顶面或图像,应该会看到一个弹出框,让用户浏览然后选择另一个图像。当个人资料屏幕被关闭时(从弹出窗口中的“取消”或“选择”),主屏幕应该使用用户在弹出窗口中选择的照片来更新照片。

现在,我正在使用委托方法将弹出窗口中的用户选择返回给调用 ViewController,然后它会自行关闭。问题是当我运行我的代码时,在调用 ViewController 移动到下一步并尝试使用配置文件屏幕在被关闭之前更新的变量之前,弹出框甚至不会出现。这是一个问题,因为该变量是垃圾,直到用户在弹出窗口中执行某些操作来更新它。

代码的工作方式如下。首先,用户在主 ViewController 中点击一些东西:

- (IBAction)handleTapFace1:(UITapGestureRecognizer *)sender
{
    NSLog(@"Face 1 tapped!");
    [self showProfileView:0];

    if (returnedSwitchIndex != NO_SWITCH)
    {
        // Animate switch
        [self performVisualSwitch: selectedFaceIndex to:returnedSwitchIndex];
    }
}

showProfileView 是我在其中启动弹出配置文件窗口的功能,其中包含点击面部的详细信息,以及浏览每个面部以查找替换面部的能力。弹出框包含一个 Cancel UIButton、一个 Switch UIButton 以及详细信息。

-(void) showProfileView: (int) tappedIndex
{
    UIStoryboard *storyboard = self.storyboard;

    ProfileViewController *profViewController = [storyboard instantiateViewControllerWithIdentifier:@"ProfileView"];

    // Pass the parameters profView needs to display
    profViewController.currentIndex = tappedIndex;
    profViewController.turnIndex = 0; // <-- TODO: test value

    NSMutableArray* members = [[NSMutableArray alloc] init];
    ... load array with candidate faces ...

    profViewController.faces = [[NSArray alloc] initWithArray:members];

    [self addChildViewController:profViewController];

    profViewController.view.frame = CGRectMake(0, 0, 320, 548);
    [self.view addSubview: profViewController.view];
    profViewController.view.alpha = 0;
    [profViewController didMoveToParentViewController:self];

    NSLog(@"Self located at: %@", NSStringFromCGPoint(self.view.frame.origin));
    NSLog(@"Frame located at: %@", NSStringFromCGPoint(profViewController.view.frame.origin));

    // popup
    [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^
     {
         profViewController.view.alpha = 1;
     }
                     completion:nil];

    // I previous tried to return a value here, that also failed
    //return profViewController.switchIndex;

    // trying to force redisplay before calling ViewController can proceed
    [profViewController.view setNeedsDisplay];
}

我想我想问的是......“为什么 UIView/popover 窗口不能在 showProfileView 的调用者前进之前出现?”和“在调用 ViewController 超出 showProfileView 行并开始使用返回的SwitchIndex 之前,如何强制用户与弹出的 UIView 或 UIViewController 进行交互?”也许我的委托解决方案不是这样,但是我如何从显示的 UIView 返回一些东西?

感谢您的帮助。抱歉所有问题。

PS> 我选择使用动画 UIView 方法是因为我需要调用 ViewController 继续存在并显示在后台,并且它还包含一个 NSTimer 在用户使用弹出视图时滴答作响。可以并且希望调用 ViewController 在那里并且它的 NSTimer 继续运行,但是在视图有机会实际出现并强制用户与之交互之前,在代码中向前移动到 showProfileView 之外是完全不好的(或者按下取消按钮或浏览并按下另一个面上的切换按钮)

【问题讨论】:

    标签: ios iphone user-interface uiview uiviewcontroller


    【解决方案1】:

    我不确定你的要求是什么。但是您必须了解 UIView 操作是一次性执行的。

    所以:

    用户触摸一张脸:

    - (IBAction)handleTapFace1:(UITapGestureRecognizer *)sender
    {
        NSLog(@"Face 1 tapped!");
        [self showProfileView:0];
    }
    

    然后你把回调放在另一个方法中:

    - (void)goAhead
    {
        if (returnedSwitchIndex != NO_SWITCH)
        {
            // Animate switch
            [self performVisualSwitch: selectedFaceIndex to:returnedSwitchIndex];
        }
    }
    

    最后你在需要时调用 goAhead 方法(在用户与 popover 交互之后),例如:

    - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
    {
        [self goAhead];
    }
    

    【讨论】:

      【解决方案2】:

      拉这玩意

      if (returnedSwitchIndex != NO_SWITCH)
      {
          // Animate switch
          [self performVisualSwitch: selectedFaceIndex to:returnedSwitchIndex];
      }
      

      输出到自己的方法中,并在用户选择或取消时将该方法设置为目标。应该可以的。

      要让 UIButton 调用您的方法,您可以执行以下操作:

      [button addTarget:self
                 action:@selector(userDidChoose)
       forControlEvents:UIControlEventTouchUpInside];
      

      【讨论】:

      • 是的,成功了!谢谢。上述解决方案也有效,本质上是相同的,如果可以的话,我会指定两个答案。再次感谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 2016-10-25
      • 2013-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多