【问题标题】:How to send array to subview and then print into label?如何将数组发送到子视图然后打印到标签中?
【发布时间】:2012-05-03 05:32:08
【问题描述】:

处理故事板。

我创建了视图控制器(故事板),然后在内容中间从 xib 文件添加了子视图。

我想将 xib (UIView) 作为子视图添加到 ViewController 中并发送带有数据的对象并将该数据打印到标签中,但我不知道如何。

这是我的代码。

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    UIView *subView;
}

@property (strong, nonatomic) IBOutlet UIView *subView; //connected over IB
@end

ViewController.m

#import "OfferViewController.h"
#import "OfferLocation.h"

@interface OfferViewController ()

@end

@implementation OfferViewController

@synthesize subView;

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    OfferLocation *location = [[OfferLocation alloc] initWithFrame:CGRectZero];
    [self.subView addSubview:location];
}
...
@end

这是子视图: OfferLocation.h

#import <UIKit/UIKit.h>

@interface OfferLocation : UIView{
    UIView *view;
    UILabel *locationLabel;// here is that label taht I want to print into
}

@property (nonatomic, retain) IBOutlet UIView *view;// connected over IB
@property (nonatomic, retain) IBOutlet UILabel *locationLabel;

@end

OfferLocation.m

#import "OfferLocation.h"

@implementation OfferLocation

@synthesize view, locationLabel;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        locationLabel.text = @"some text"; //this is not working
        NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"OfferLocation" owner:self options:nil];
        UIView *tempView = [subviewArray objectAtIndex:0];
        [self addSubview:tempView];
    }
    return self;
}

【问题讨论】:

  • 我创建了新的 UIVIewController (OfferLocation) 而不是 UIView,然后只编辑这个新视图控制器的顶部并作为子视图添加到 ViewController.h 的中间。那是对的吗? OfferLocation (UIVIewController) 是:dl.dropbox.com/u/77033905/subview%20ss.png 我使用绿色部分作为子视图。
  • 这不太好,我想出正确的方法。在 IB 中,我刚刚删除了默认视图并从对象库中添加了 UIView,现在一切正常。我有人在这个问题上需要帮助,请随时提出,我会提供帮助。

标签: objective-c xcode storyboard subview


【解决方案1】:

subView 似乎永远不会被初始化并加载到视图或呈现。所以你看不到它。

尝试调用:

            self.view = self.subView // or subView; // or [self.view addSubview:self.subView // or subview];

或类似的东西

然后声明一个 UILabel 并使用您想要的任何文本对其进行初始化并将其添加到子视图:

         UILabel *label = [[UILabel alloc] initWithString:@"StackOverflow rocks!"];
         [self.subView addSubview:label];
// or [self.view addSubview:label];

设置子视图的方式不同。

您想在标签的文本上设置什么数据?如果是数据类型,则使用字符串中的说明符。

        %i // for int
%@ // for string or object adress
%c // for char
%f // for float

等等……

【讨论】:

  • subView 我通过 IB 创建并连接,它正在工作。上面的子视图正在工作,但我不知道如何将数组发送到子视图以及如何在我已经创建并通过 IB 连接的标签中定义文本
  • 我在第一篇文章中的代码可以正常工作,除了将数组发送到我不知道该怎么做的那个子视图,如果你查看“OfferLocation.m”,你会看到“locationLabel.text = @"some text"; //这不起作用 ".此标签是通过 IB 创建并连接/合成的,但我无法为该标签设置文本。我有这两个问题。 Tnx 帮助我解决问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-13
  • 1970-01-01
  • 2015-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多