简体   繁体   English

为什么从uibutton而不是从手势调用时方法失败?

[英]Why method fail when called from uibutton but not from a gesture?

I'm new to ios programming and I'm stuck with an error. 我是ios编程的新手,但我遇到了错误。 My app is for a magazine and I use a UIScrollView to see the articles. 我的应用程序是用于杂志,并且我使用UIScrollView来查看文章。

In my app I have 2 ways to navigate in a UIScrollView, using a Gesture and with 2 buttons, because I change the content of the scrollview depending on page and use a method to go forward and one to go backwards, inside them I use a If to evaluate if it was the first page or the last page and disable the buttons depending on the case. 在我的应用程序中,我有两种使用手势和2个按钮在UIScrollView中导航的方法,因为我根据页面更改了滚动视图的内容,并使用一种前进和后退的方法,在其中使用了一种是否评估是第一页还是最后一页,并根据情况禁用按钮。

The first time i access the view with this methods everything is fine but on a second access the buttons fail but the gesture works fine, is there a difference when calling a method from a button or a gesture? 第一次使用此方法访问视图时,一切都很好,但是第二次访问时,按钮失败,但手势正常,从按钮或手势调用方法时有区别吗?

Here is the code: 这是代码:

//Here is where I assign the method to the buttons and gesture
[articuloAnterior addTarget:self action:@selector(cambiarPaginaAnterior) forControlEvents:UIControlEventTouchUpInside];

[articuloSiguiente addTarget:self action:@selector(cambiarPaginaSiguiente) forControlEvents:UIControlEventTouchUpInside];

UISwipeGestureRecognizer *avanzarPagina = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(cambiarPaginaSiguiente)];
avanzarPagina.direction = UISwipeGestureRecognizerDirectionLeft;

UISwipeGestureRecognizer *regresaPagina = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(cambiarPaginaAnterior)];
regresaPagina.direction = UISwipeGestureRecognizerDirectionRight;

//Here are the functions
-(void)cambiarPaginaAnterior{
//This method checks if it is the first page, in case it isn't it go one page back
NSLog(@"Pagina actual %d de %d",paginaMostrada,[appDelegate.seccionActual.listadoArticulos count]-1);
if (0 != paginaActual) {
    paginaMostrada = paginaActual - 1;
    NSLog(@"Pagina actual %d de %d",paginaMostrada,[appDelegate.seccionActual.listadoArticulos count]-1);
    [self cambiarLimites];
  }
}

-(void)cambiarPaginaSiguiente{
//In this method I check the actual page (paginaActual) to see if it is the last one, if it isn't then it go forward
NSInteger limiteDePaginacion = [appDelegate.seccionActual.listadoArticulos count] - 1;
 NSLog(@"Pagina actual %d de %d",paginaMostrada,limiteDePaginacion);
if (limiteDePaginacion != paginaActual) {
    paginaMostrada = paginaActual + 1;
    NSLog(@"Pagina actual %d de %d",paginaMostrada,limiteDePaginacion);
    [self cambiarLimites];
  }
}

-(void)cambiarLimites{
//Here i save the point where the user was located and save it in an array
NSValue *posicionActualDeLaVista = [NSValue valueWithCGPoint:CGPointMake(0, contenedorGeneral.contentOffset.y)];
[posicionDeLaPagina removeObjectAtIndex:paginaActual];
[posicionDeLaPagina insertObject:posicionActualDeLaVista atIndex:paginaActual];

//Here I change the value of the actual page
paginaActual=paginaMostrada;

//Here I disable the buttons or make them able
if (paginaActual==([appDelegate.seccionActual.listadoArticulos count]-1)) {
    articuloSiguiente.enabled = NO;
}
else {
    articuloSiguiente.enabled = YES;
}
if (paginaActual==0) {
    articuloAnterior.enabled = NO;
}
else {
    articuloAnterior.enabled = YES;
}

//A log to check the state of the buttons
NSLog(@"Pagina actual %d estado del boton anterior %@, estado del boton siguiente %@",paginaActual,articuloAnterior.enabled?@"yes":@"no",articuloSiguiente.enabled?@"yes":@"no");

//Acces an array with the points the user where
CGPoint puntoDeVisualizacion = [[posicionDeLaPagina objectAtIndex:paginaActual]CGPointValue];

//Here I remove the content of the uiscrollview and put the new one
[vistaTemporale removeFromSuperview];
vistaTemporale = [vistasParaElScroll objectAtIndex:paginaActual];
vistaTemporale = [self ResizeDeLaVista:vistaTemporale laPagina:paginaActual];
[contenedorGeneral addSubview:vistaTemporale];
[contenedorGeneral setContentOffset:puntoDeVisualizacion animated:NO];

//Check the view height and change the UIScrollView content height
float limitanteDeAltura;
limitanteDeAltura = [[appDelegate.viewSizes objectAtIndex:paginaActual]floatValue];
[contenedorGeneral setContentSize:CGSizeMake(anchoDeVista, limitanteDeAltura)];
}

I made my own gesture because I need to negate the diagonal scrolling so I thought it was a good idea to only show one view at a time, but the gesture is not the one failing, the buttons are not getting disabled, they are declared in the .h file and in the first access they work fine, any idea why are they failing? 我做出了自己的手势,因为我需要取消对角线滚动,所以我认为一次只显示一个视图是一个好主意,但是手势并不是一个失败,按钮没有被禁用,它们被声明为.h文件,并且在首次访问时它们可以正常工作,知道它们为什么会失败吗?

感谢您的帮助,但问题是我在工具栏中添加了按钮,并且由于内存管理错误,按钮仍在此处,因此新按钮处于禁用状态,而旧按钮处于可见和活动状态,对此问题表示抱歉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何发送方法以从UIButton调用的操作进行查看 - How to send method to view from action called by UIButton 为什么不调用UIButton的选择器方法? - Why UIButton's selector method is not called? 为什么在终端中使用curl调用时POST无法正常工作? - Why does POST work when using a curl call in terminal fail when called from I Swift-按下UIButton时不会调用其他类的函数 - Swift - Function from another class is not called when UIButton is pressed 具有平移手势和按下方法的UIButton - UIButton with pan gesture and press method 为什么 SwiftUI opacity animation 在 Gesture 的 onEnded 方法中失败? - Why does SwiftUI opacity animation fail in Gesture's onEnded method? 如何有条件地将手势从UIButton“传递”到UIScrollView? - How to conditionally “pass through” a gesture from a UIButton to a UIScrollView? 方法调用时,以编程方式崩溃的UIButton崩溃了 - UIButton that's created programmatically crashing when method called 当button是UICollectionViewCell的子视图时,不会调用UIButton的目标方法 - Target method for UIButton not called when button is subview of UICollectionViewCell 从后台调用应用程序时调用方法 - Calling a method when the application is called from background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM