提问人:user2727854 提问时间:8/29/2013 最后编辑:user2727854 更新时间:8/29/2013 访问量:643
-[UIImage direction]:发送到实例的无法识别的选择器
-[UIImage direction]: unrecognized selector sent to instance
问:
我的要求是,如果我单击button1,相应的图像必须通过滑动手势进入下一个视图,因此我将发送者值发送到子视图控制器
viewcontroller.m
-(IBAction)cricket:(id)sender
{
//cricket is a button
NSString *str=[cricket currentTitle];
ChildViewController *child=[[ChildViewController alloc]init];
[child handleSwipe:str];
}
这里是
childviewcontroller.m
int imageIndex=4;
- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)sender
{
if([sender isEqual:@"cricket"]))
{
NSArray *images=[[NSArray alloc]initWithObjects:[UIImage imageNamed:@"exterior1.png"], [UIImage imageNamed:@"cricket.png"],[UIImage imageNamed:@"floor_plan1.png"],Nil];
UISwipeGestureRecognizerDirection direction=[(UISwipeGestureRecognizer *)sender direction];
switch (direction) {
case UISwipeGestureRecognizerDirectionLeft:
imageIndex++;
break;
case UISwipeGestureRecognizerDirectionRight:
imageIndex--;
default:
break;
}
imageIndex=(imageIndex<0) ? ([images count]-1):
imageIndex % [images count];
[images objectAtIndex:imageIndex];
}
我被这个东西吓坏了,请帮帮我
答:
这里不是方法,唯一的方法就是应用程序崩溃的原因。direction
getter
setter
在这里,它给出了图像视图对象,一旦检查你的代码。(UISwipeGestureRecognizer *)sender
Edit:-
我们可以使用 direction 属性为 UISwipeGestureRecognizer 设置方向。如果你像这样尝试 _swiper.direction == UISwipeGestureRecognizerDirectionLeft 每次都会出错,因为在这里你试图获取方向,但它不起作用。
为了识别滑动方向,您可以为不同的方向添加不同的手势。direction 属性仅定义被识别为轻扫的允许方向,而不定义特定轻扫的实际方向。
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromLeft)];
[leftRecognizer setDirection: UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromRight)];
[rightRecognizer setDirection: UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
EDIT:
-(void)handleSwipeFromRight{
NSLog(@"swipe from right");
}
and
-(void)handleSwipeFromLeft{
NSLog(@"swipe from Left");
}
评论
我真的不明白你想做什么,但是你的代码有很多问题,让我从这里开始:
NSString *str= [arr objectAtIndex:0];
您正在从数组中获取第一个对象并将其放在 .NSString
UIImage
然后在你中,你正在检查发件人是否等于另一个,但这里的等于将检查指针,并且由于通常会返回缓存的图像,这可能会起作用。但它可能不会。childviewcontroller.m
UIImage
[UIImage imageNamed:]
将 The cast to :sender
UISwipeGestureRecognizer
UISwipeGestureRecognizerDirection direction=[(UISwipeGestureRecognizer *)sender direction];
但是,如果您正在检查发送者是否是,然后将其转换为.但事实并非如此。你的代码崩溃了。UIImage
UISwipeGestureRecognizer
然后你这样做:
img.image=[UIImage imageNamed:[images objectAtIndex:imageIndex]];
返回的对象已经是 a,在这种情况下将始终返回。您可以只使用:[images objectAtIndex:imageIndex]
UIImage
[UIImage imageNamed:[images objectAtIndex:imageIndex]]
nil
img.image= [images objectAtIndex:imageIndex];
我建议你重写代码,只使用字符串而不是对象,因为它们会占用更多的内存。然后是最后一步,使用方法加载图像。UIImage
imageNamed:
评论