Segue From Custom UIView(从警报视图到UIViewController)



我在UIViewController上有一个按钮,点击该按钮,就会弹出一个UIview,就像alertview一样,里面有表格视图。现在,在选择表格单元格时,我想转到详细视图控制器

这是我引用的链接,但它们都不适用于我

对于alertview,我使用了(https://github.com/kwent/ios-custom-alertview)

谢谢。

@yar1vn的答案是正确的,但是,我将更准确地描述您需要做什么。

链接中的自定义警报视图具有delegate属性,该属性应符合协议

@protocol CustomIOS7AlertViewDelegate
- (void)customIOS7dialogButtonTouchUpInside:(id)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
@end

这意味着您应该在UIViewController中实现此方法。

in.h文件:

@interface YourViewController : UIViewController <YourViewController>
...
@end

in.m文件:

@implementation YourViewController
...
- (void)customIOS7dialogButtonTouchUpInside:(id)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self performSegueWithIdentifier:@"YourSegue" sender:nil];
}

并在创建alertView:时设置代理

[alertView setDelegate:self];

@"YourSegue"是从显示alertView的控制器到详细视图控制器的分段。

我不同意你应该使用UIAlertController,因为如果你的部署目标是iOS 7(这是合理的),你就不应该使用iOS 8 的新功能

编辑:

如果您想从表视图单元格上的点击启动segue,您应该从tableView的委托方法-tableView:didSelectRowAtIndexPath: 调用[self performSegueWithIdentifier:@"YourSegue" sender:nil]

我假设您已经将当前视图控制器设置为tableView的dataSource和delegate,所以添加到您的视图控制器中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"YourSegue" sender:nil];
}

编辑2:

尽管将UIView设置为委托不是最好的方法,但我们可以处理它:)

我看到两种解决方案:

第一个是将控制器作为属性添加到您的视图中,如下所示:

@interface YourView : UIView <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, weak) YourViewController *parentController;
...

在某个地方(可能在-viewDidLoad中),您将此属性设置为

youViewInstance.parentController = self;

和视图中的委托方法调用

[self.parentController performSegueWithIdentifier:@"YouSegue" sender:nil]

第二种方法是简单地将控制器设置为tableView的委托,并从其方法调用performSegue:。您应该更完整地描述所有细节:)

您不应该再使用第三方AlertViews了。您可以使用iOS 8 SDK附带的AlertController

我不知道这个AlertView是如何工作的,但自述中提到了一位代表。您是否尝试过从委托方法调用segue?

最新更新