0

在 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 之外是完全不好的(或者按下取消按钮或浏览并按下另一面的切换按钮)

4

2 回答 2

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 方法,例如:

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    [self goAhead];
}
于 2013-10-23T09:48:12.580 回答
1

拉这个东西

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

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

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

[button addTarget:self
           action:@selector(userDidChoose)
 forControlEvents:UIControlEventTouchUpInside];
于 2013-10-23T09:49:35.177 回答