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

时间:2012-05-30 22:32:13

标签: ios methods

我是ios编程的新手,我遇到了错误。我的应用程序用于杂志,我使用UIScrollView查看文章。

在我的应用程序中,我有2种方式在UIScrollView中导航,使用手势和2个按钮,因为我根据页面更改了scrollview的内容并使用方法前进,然后使用方法向后移动,在其中我使用If来评估它是第一页还是最后一页,并根据具体情况禁用按钮。

我第一次使用这种方法访问视图时一切都很好但是在第二次访问时按钮失败但手势工作正常,从按钮或手势调用方法时有区别吗?

以下是代码:

//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)];
}

我做了我自己的手势,因为我需要否定对角线滚动,所以我认为一次只显示一个视图是一个好主意,但手势不是失败的按钮,按钮没有被禁用,他们在.h文件中声明,并且在第一次访问时它们工作正常,任何想法为什么它们失败了?

1 个答案:

答案 0 :(得分:0)

感谢您的帮助,但问题是我在工具栏中添加按钮,因为内存管理错误按钮仍然在那里,所以新按钮禁用但旧的按钮可见且活动,抱歉问题