1

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

在我的应用程序中,我有 2 种方式在 UIScrollView 中导航,使用手势和 2 个按钮,因为我根据页面更改滚动视图的内容并使用一种方法前进和一种后退,在它们内部我使用如果要评估它是第一页还是最后一页,并根据情况禁用按钮。

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

这是代码:

//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 文件并在第一次访问时工作正常,知道为什么会失败吗?

4

1 回答 1

0

Thanks for the help but the problem was that I was adding the buttons in the toolbar and because a memory management error the buttons still where there, so the new buttons where disabled but the old ones where visible and active, sorry for the problem

于 2012-05-31T18:58:41.103 回答