iphone – UIScrollViewDelegate没有开火

iphone – UIScrollViewDelegate没有开火,第1张

概述这可能是一些 sort of duplicate of this question.但我试图应用它的答案无济于事. 我有一个带有pagingEnabled的UIScrollView.我想使用UIScrollViewDelegate来帮助检测我所在的页面.这是我的代码: @interface SearchMissionViewController : UIViewController <UIScro 这可能是一些 sort of duplicate of this question.但我试图应用它的答案无济于事.

我有一个带有pagingEnabled的UIScrollVIEw.我想使用uiscrollviewdelegate来帮助检测我所在的页面.这是我的代码:

@interface SearchMissionVIEwController : UIVIEwController <uiscrollviewdelegate> {    UIScrollVIEw *scroll;}    @property (nonatomic,retain) UIScrollVIEw *scroll;    - (voID)scrollVIEwDIDEndDecelerating:(UIScrollVIEw *)scrollVIEw;    - (voID)scrollVIEwDIDScroll:(UIScrollVIEw *)scrollVIEw;    - (IBAction) popVIEw;    @end- (voID)vIEwDIDLoad{    [super vIEwDIDLoad] ;    // Here I have also trIEd scroll.delegate = self;    self.scroll.delegate = self;    self.vIEw.backgroundcolor = [UIcolor redcolor];    scroll = [[UIScrollVIEw alloc] initWithFrame:CGRectMake(0,self.vIEw.frame.size.wIDth,self.vIEw.frame.size.height)];    // Is this correct? I've added it to try to get the UIScrollVIEw Delegate calling scrollVIEwDIDEndScrollingAnimation (or whatever)    // [scroll setContentOffset:CGPointMake(0.0,0.0) animated:YES];    scroll.pagingEnabled = YES;    NSInteger numberOfVIEws = 3;    for (int i = 0; i < numberOfVIEws; i++) {        CGfloat yOrigin = i * self.vIEw.frame.size.wIDth;        UIVIEw *awesomeVIEw = [[UIVIEw alloc] initWithFrame:CGRectMake(yOrigin,self.vIEw.frame.size.height)];        awesomeVIEw.backgroundcolor = [UIcolor colorWithRed:0.9 green:0.9 blue:0.9 Alpha:1];        UIImageVIEw *targetVIEw = [[UIImageVIEw alloc] initWithImage:[UIImage imagenamed:@"targetimageA.png"]];        targetVIEw.frame = CGRectMake(0.0,0.0,139,153);        targetVIEw.frame = CGRectMake(yOrigin+90,80,153);        UILabel *targetnameLabel = [[UILabel alloc] initWithFrame:CGRectMake(yOrigin+95,230,100,20)];        targetnameLabel.text = @"Bond";        targetnameLabel.backgroundcolor = [UIcolor clearcolor];        targetnameLabel.textcolor = [UIcolor redcolor];        UILabel *missionnameLabel = [[UILabel alloc] initWithFrame:CGRectMake(yOrigin+95,250,20)];        missionnameLabel.text = @"Mission";        missionnameLabel.backgroundcolor = [UIcolor clearcolor];        missionnameLabel.textcolor = [UIcolor redcolor];        [scroll addSubvIEw:awesomeVIEw];                [scroll addSubvIEw:targetVIEw];        [scroll addSubvIEw:targetnameLabel];         [scroll addSubvIEw:missionnameLabel];        // Why does this get released? Wouldn't it then dissappear from the scroll vIEw? Should I not be releasing other objects from above?        [awesomeVIEw release];    }    scroll.contentSize = CGSizeMake(self.vIEw.frame.size.wIDth * numberOfVIEws,self.vIEw.frame.size.height);    [self.vIEw addSubvIEw:scroll];    [scroll release];     // Overlap image    UIImageVIEw *imgVIEw = [[UIImageVIEw alloc] initWithFrame:CGRectMake(0,320,460)];    imgVIEw.image= [UIImage imagenamed:@"mission-vIEwfinder.png"];    [self.vIEw addSubvIEw:imgVIEw];    // Place back button ontop of the image.     UIbutton *button = [UIbutton buttonWithType:UIbuttonTypeCustom ];    [button addTarget:self                action:@selector(popVIEw)    forControlEvents:UIControlEventtouchDown];    button.frame = CGRectMake(0.0,100.0,60.0);    [self.vIEw addSubvIEw:button];    // Do any additional setup after loading the vIEw from its nib.}- (voID)scrollVIEwDIDEndDecelerating:(UIScrollVIEw *)scrollVIEw{    NSLog(@" ");    NSLog(@" Scroll VIEw DID End Decelerating");    NSLog(@" ");  }- (voID)scrollVIEwDIDScroll:(UIScrollVIEw *)scrollVIEw{    NSLog(@" ");    NSLog(@" Scroll VIEw DID End Scroll ! ! ! ");    NSLog(@" ");  }

本质上问题是上面的两个方法,scrollVIEwDIDEndDeclerating和scrollVIEwDIDScroll都没有打印.换句话说,他们没有被召唤.

有任何想法吗?

解决方法 问题在于陈述的顺序:

self.scroll.delegate = self;...scroll = [[UIScrollVIEw alloc] initWithFrame:CGRectMake(0,self.vIEw.frame.size.height)];

您尝试在创建scrollvIEw之前设置委托,因此该语句无效,因为scroll为nil.改为:

self.scroll = [[[UIScrollVIEw alloc] initWithFrame:CGRectMake(...)] autorelease];self.scroll.delegate = self;

我刚注意到你的评论:

// Why does this get released? Wouldn't it then dissappear from the scroll vIEw? Should I not be releasing other objects from above?[awesomeVIEw release];

是的,您应该在将它们添加到scrollVIEw后释放targetVIEw,targetnameLabel和missionnameLabel.您拥有它们是因为您创建了它们(通过alloc-init),您必须通过向它们发送释放消息来放弃它们的所有权.当您将它们添加到scrollVIEw时,它将保留它们,如addSubvIEw文档中所述:以确保它们在scrollVIEw需要时不会被释放.

总结

以上是内存溢出为你收集整理的iphone – UIScrollViewDelegate没有开火全部内容,希望文章能够帮你解决iphone – UIScrollViewDelegate没有开火所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1035116.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存