我的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上,所以我从app delegate中获取了navigationController。在我的DropDownPickerViewController代码中,当用户选择记录时,我有以下内容:
[[(AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate] navigationController] dismissModalViewControllerAnimated:NO];
但没有任何事情发生。它似乎冻结并挂起,不允许与表单进行任何交互。关于为什么我的ModalViewController不会被解雇的任何想法?我认为这是我使用app委托来呈现viewController的方式。有没有办法在UIButton类中使用self.ParentViewController,这样我就可以获得按钮所在视图的ViewController(如果这是问题)?
非常感谢任何想法。
由于
答案 0 :(得分:0)
在DropDownPickerViewController中,而不是:
[[(AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate] navigationController] dismissModalViewControllerAnimated:NO];
尝试
[self dismissModalViewControllerAnimated:NO];
答案 1 :(得分:0)
我会在按钮的“父”viewController上调用一个方法,然后从那里开始。
答案 2 :(得分:0)
我确实记得我曾经做过一次类似的事情,看看这篇文章:
Get to UIViewController from UIView?
我能够创建一些类别:
//
// 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];
也许他们会帮助你,我希望如此。