1

我的 iPhone 导航应用程序中有一个自定义 UIButton 类。当用户单击按钮时,我会显示一个模式视图,该视图显示用户可以从中选择的记录的 tableview 列表。

单击按钮后,我创建了 viewController 并使用以下代码显示它:

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    dropDownPickerViewController = [[DropDownListingViewController alloc] initWithNibName:@"DropDownListingView" bundle:[NSBundle mainBundle]];
    .....
    .....

[[(AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate] navigationController] presentModalViewController:dropDownPickerViewController animated:NO];
[dropDownPickerViewController release];

[super touchesEnded:touches withEvent:event];
}

如您所见,该按钮可以在任何 viewController 上,因此我从应用程序委托中获取了 navigationController。比在我的 DropDownPickerViewController 代码中,当用户选择记录时,我有以下内容:

[[(AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate] navigationController] dismissModalViewControllerAnimated:NO];

但什么也没有发生。它似乎冻结并挂起,不允许与表单进行任何交互。关于为什么我的 ModalViewController 不会被解雇的任何想法?我认为这是我使用应用程序委托来呈现 viewController 的方式。有没有办法在 UIButton 类中使用 self.ParentViewController 以便我可以获得按钮所在视图的 ViewController(如果这是问题所在)?

任何想法将不胜感激。

谢谢

4

3 回答 3

0

在你的 DropDownPickerViewController 中,而不是这个:

[[(AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate] navigationController] dismissModalViewControllerAnimated:NO];

尝试

[self dismissModalViewControllerAnimated:NO]; 
于 2011-10-03T06:03:06.603 回答
0

我会在按钮的“父”视图控制器上调用一个方法,然后从那里开始。

于 2011-10-03T15:19:12.083 回答
0

我确实记得我确实必须做一次类似的事情,并且通过查看这篇文章:

从 UIView 到 UIViewController?

我能够创建一些类别:

//
//  UIKitCategories.h
//

#import <Foundation/Foundation.h>

@interface UIView (FindUIViewController)
- (UIViewController *) firstAvailableUIViewController;
- (id) traverseResponderChainForUIViewController;
@end

@interface UIView (FindUINavigationController)
- (UINavigationController *) firstAvailableUINavigationController;
- (id) traverseResponderChainForUINavigationController;
@end




//
//  UIKitCategories.m
//

#import "UIKitCategories.h"

@implementation UIView (FindUIViewController)
- (UIViewController *) firstAvailableUIViewController {
    // convenience function for casting and to "mask" the recursive function
    return (UIViewController *)[self traverseResponderChainForUIViewController];
}

- (id) traverseResponderChainForUIViewController {
    id nextResponder = [self nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        return nextResponder;
    } else if ([nextResponder isKindOfClass:[UIView class]]) {
        return [nextResponder traverseResponderChainForUIViewController];
    } else {
        return nil;
    }
}

@end

@implementation UIView (FindUINavigationController)
- (UINavigationController *) firstAvailableUINavigationController {
    // convenience function for casting and to "mask" the recursive function
    return (UINavigationController *)[self traverseResponderChainForUINavigationController];
}

- (id) traverseResponderChainForUINavigationController {
    id nextResponder = [self nextResponder];
    if ([nextResponder isKindOfClass:[UINavigationController class]]) {
        return nextResponder;
    } else {
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            //NSLog(@" this is a UIViewController ");
            return [nextResponder traverseResponderChainForUINavigationController];
        } else {
            if ([nextResponder isKindOfClass:[UIView class]]) {
                return [nextResponder traverseResponderChainForUINavigationController];
            } else {
                return nil;
            }
        }
    }
}

@end

然后你可以这样称呼他们:

UINavigationController *myNavController = [self firstAvailableUINavigationController];

UIViewController *myViewController = [self firstAvailableUIViewController];

也许他们会帮助你,我希望如此。

于 2011-10-04T12:31:58.930 回答