• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - 无限循环添加 subview

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:28:32 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有点迷茫,经过无数次尝试和阅读了几篇文章,我决定写。 我的问题是,如果你从一个类(xml)中调用一个方法并且它是针对 viewcontroller 的,一切顺利 但如果我可以添加 [self.view add...] 它回到顶部重新加载 viewController 类的 viewDidLoad 进入无限循环。

这就是我的工作

类( View Controller ) .h

#import <UIKit/UIKit.h>

@class  XMLStuff;

@interface skiSpeedViewController : UIViewController {
}

@property (nonatomic, retain) XMLStuff *xml;

.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    xml.skiSpeedC = self;

    GpsStuff *gps = [GpsStuff alloc];
    [gps init];
}

gps.m

- (id)init
{
    self = [super init];
    if (self) {
        xml = [XMLStuff alloc];
}
}
-(void)locationManagerCLLocationManager *)manager didUpdateToLocationCLLocation *)newLocation fromLocationCLLocation *)oldLocation {
     [xml lon:newLocation.coordinate.longitude lat:newLocation.coordinate.latitude];

xml.h

#import "skiSpeedViewController.h"

@class skiSpeedViewController;

@interface XMLStuff : NSObject <NSXMLParserDelegate> {
}

@property (retain, nonatomic) skiSpeedViewController *skiSpeedC;

.m

   @synthesize skiSpeedC;

- (void) parserDidEndDocumentNSXMLParser *)parser {
NSLog(@"--%@", self.skiSpeedC); // Return (null)
[self.skiSpeedC riceviDic:datiMeteo];
}

ViewController.m

    -(void)riceviDicNSMutableDictionary *)dictMeteo {
        datiMeteo = [[NSMutableDictionary alloc]initWithDictionary:dictMeteo];
    }
}



Best Answer-推荐答案


- (void) parserDidEndDocumentNSXMLParser *)parser {
    classViewController *skiSpeedC = [classViewController alloc];
    [skiSpeedC riceviDic:datiMeteo];
}

您每次都在创建 classViewController 的新实例。您的“xml”类(XMLStuff?)应该有一个指向 View Controller 的指针,并在该实例上调用 riceviDic 方法。

你得到了一个无限循环,因为当你在 viewDidLoad 中分配 XML 对象时,它也开始解析 XML,然后创建更多的 XML 对象,然后创建更多的 viewControllers...

所以,给 XMLStuff 添加一个 classViewController 类型的属性,当你在 viewDidLoad 中创建它时:

xml.skiSpeedC = self;

然后,在 parserDidEndDocument 中:

- (void) parserDidEndDocumentNSXMLParser *)parser {
    [self.skiSpeedC riceviDic:datiMeteo];
}

更新

好的,在你编辑之后,事情看起来很不一样 - 你似乎引入了一个新类 - GpsStuff,它有它自己的自己的 XMLStuff 实例(以及一个看起来很狡猾的 init 方法,我假设你没有'没有正确复制?)。哪一个实际上在解析您的文档? XMLStuff 在您的 View Controller 中,还是在 GPSStufF 中?我猜是 GPSStuff 中的那个,您没有为其设置 skiSpeedC 属性。我之前假设您从 View Controller 调用所有内容。

为什么不从 GPSStuff 中删除新 XMLStuff 对象的创建,当您在 View Controller 中创建 GPSStuff 时,将您创建的 xml 对象传递给它:

- (void)viewDidLoad
{
    [super viewDidLoad];

    GpsStuff *gps = [[GpsStuff alloc] init];
    XMLStuff *xml = [[XMLStuff alloc] init];
    xml.skiSpeedC = self;
    gps.xml = xml;
    [xml release];
}

另外,skiSpeedC 属性可能不应该是 retain,因为它本质上是一个委托(delegate)分配,并且在您释放 xml 之前不会释放 View Controller 解析器。

请注意,按照惯例,您应该像这样初始化对象:

GPSStuff *gps = [[GPSStuff alloc] init];

不在两行。您希望将 init 返回的内容分配给您的变量。

关于iphone - 无限循环添加 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7809229/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap