【问题标题】:UILabel method won't returnUILabel 方法不会返回
【发布时间】:2014-09-17 04:22:02
【问题描述】:

我不断收到声明隐藏实例变量,但我不知道在哪里调用它。我正在尝试以编程方式引用它。我以编程方式创建了 UILabel,所以我没有任何东西可以将它拖到 .xib 中

MapViewController.h

#import <UIKit/UIKit.h>
#import "RESideMenu.h"
#import <MapKit/MapKit.h>

@interface MapViewController : UIViewController
{
IBOutlet MKMapView *mapView;
IBOutlet UISegmentedControl *segmentedControl;
IBOutlet UILabel *parseLabel;
}


@property (nonatomic, retain) MKMapView *mapView;
@property (nonatomic, retain) UISegmentedControl *segmentedControl;
@property (nonatomic, retain) IBOutlet UILabel *parseLabel;

- (IBAction)segmentedControllChanged:(id)sender;

@end

MapViewController.m

#import "MapViewController.h"
#import "MapViewAnnotation.h"
#import "Config.h"

@interface MapViewController ()

@end

@implementation MapViewController
@synthesize mapView, segmentedControl, parseLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization

}
return self;
}

-(UILabel *)parseLabel {
UILabel *parseLabel = [[UILabel alloc]initWithFrame:CGRectMake(180, 1, 200, 40)];
NSError *error = nil;
NSString *html = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://harryhippie.com/app-currentlocationparse"] encoding:NSASCIIStringEncoding error:&error];
if(html) {
    NSLog(@"HTML %@", html);

    NSRange r = [html rangeOfString:@"<h2 class=\"font\">"];
    if (r.location != NSNotFound) {
        NSRange r1 = [html rangeOfString:@"</h2>"];
        if (r1.location != NSNotFound) {
            if (r1.location > r.location) {
                NSString *subtitleString = [html substringWithRange:NSMakeRange(NSMaxRange(r), r1.location - NSMaxRange(r))];
                NSLog(@"%@", subtitleString);
                self.parseLabel.text = subtitleString;
}
        }
    }
}
return parseLabel;
}

- (void)viewDidLoad
{
[self.navigationController.navigationBar addSubview:parseLabel];
[super viewDidLoad];
}

我认为我只是忽略了一些简单的事情,因为它是一个简单的错误。请帮忙:)

【问题讨论】:

    标签: ios variables methods uilabel


    【解决方案1】:

    这很简单,看看这个:

    在你的 .h 中你声明了

    @property (nonatomic, retain) IBOutlet UILabel *parseLabel;
    

    在你-(UILabel *)parseLabel 中你做到了:

    UILabel *parseLabel = [[UILabel alloc]initWithFrame:CGRectMake(180, 1, 200, 40)];
    

    变量名称相同。这就是您收到此错误的原因。 :)

    【讨论】:

    • 谢谢。是的,我删除了那行。但是现在我如何将 HTML 字符串派生的内容输出到导航栏中的 UILabel?
    • 无视!我回答了我自己的问题。谢谢你引导我朝着正确的方向前进。即使只回答了一半,我也会将其视为已回答!谢谢你的帮助 :) 我在 viewDidLoad { self.parseLabel = [[UILabel alloc] initWithFrame...etcetcetc 中做到了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    相关资源
    最近更新 更多