如何将手势切换回上一个视图 ios?

Posted

技术标签:

【中文标题】如何将手势切换回上一个视图 ios?【英文标题】:How to swipe gesture segue back to previous view ios? 【发布时间】:2014-01-08 05:17:56 【问题描述】:

我试图回到(QueryController)我来自的视图。 但是没有滑动动作发生...... 不知道我错过了什么。

@implementation ProfileController4


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        // Custom initialization

        UISwipeGestureRecognizer * Swiperight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swiperight:)];
        Swiperight.direction=UISwipeGestureRecognizerDirectionRight;
        [self.view addGestureRecognizer:Swiperight];

        UISwipeGestureRecognizer * Swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeleft:)];
        Swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
        [self.view addGestureRecognizer:Swipeleft];
    
    return self;




-(void)swipeleft:(UISwipeGestureRecognizer*)gestureRecognizer

    //Do what you want here



-(void)swiperight:(UISwipeGestureRecognizer*)gestureRecognizer

    //Do what you want here

    [self performSegueWithIdentifier: @"friendsBackToQuerySeg" sender: self];

//      [[UIViewController alloc] initWithNibName:@"QueryController1" bundle:nil];


【问题讨论】:

使用 Navigation Stack 并将 ViewController 推送到手势的选择器目标 viewDidLoad方法中添加initWithNibName 代码。 感谢@Dilip 等人。放入 viewDidLoad 方法修复了它。现在可以使用了。 【参考方案1】:

viewDidLoad 方法中添加您的UISwipeGestureRecognizer 代码..

- (void)viewDidLoad

    [super viewDidLoad];

    UISwipeGestureRecognizer * Swiperight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swiperight:)];
    Swiperight.direction=UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:Swiperight];

    UISwipeGestureRecognizer * Swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeleft:)];
    Swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:Swipeleft];

【讨论】:

以上是关于如何将手势切换回上一个视图 ios?的主要内容,如果未能解决你的问题,请参考以下文章

从 Tab View Controller 切换到滑动手势时的 iOS Xcode 问题

iOS 如何在滚动视图/webview 上实现点击手势以“敲出”文本字段?

如何跳过最顶层视图的手势 - iOS

如何在iOS 7中仅针对一个视图禁用后退手势

iOS:将手势传递给下面的视图

iOS解决嵌套在ScrollView中的TableView滑动手势冲突问题