ios - 如何让标签中的文本显示出来?

标签 ios objective-c uistoryboard

我正在使用表格 View 来显示不同的联系信息,根据您选择的信息,将会显示该特定资源的详细信息。我的代码可以正常工作,但我移动了项目,所以我不确定为什么我的标签没有出现。我有一份州联系人列表,然后你按下一个可以找到详细信息。这是我的代码:

StateContacts.h

#import <UIKit/UIKit.h>

@interface StateContacts : UITableViewController <UITableViewDelegate,
UITableViewDataSource>
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@end

StateContacts.m

#import "StateContacts.h"
#import "State.h"

@interface StateContacts ()

@end

@implementation StateContacts 
{
    NSArray *state;
    NSArray *num;
    NSArray *fax;
}

@synthesize tableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Initialize table data
    state = [NSArray arrayWithObjects:@"Child Abuse Hotline", @"Drug Lab Remediation", @"Prevent Suicide TN", @"TN Crime Vict. Comp.", @"TN Department of Education", @"TN Homeland Security", @"TN State Police", @"TN Supreme Court", nil];
    num = [NSArray arrayWithObjects:@"(111) 111-1111", @"(222) 222-2222", @"(333) 333-3333", @"(444) 444-4444", @"(555) 555-5555", @"(666) 666-6666", @"(777) 777-7777", @"(888) 888-8888", nil];
    fax = [NSArray arrayWithObjects:@"n/a", @"n/a", @"n/a", @"n/a", @"n/a", @"n/a", @"n/a", @"n/a", nil];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [state count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"myCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [state objectAtIndex:indexPath.row];
    return cell;
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if ([segue.identifier isEqualToString:@"showStateInfo"]) 
    {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        State *destViewController = segue.destinationViewController;
        destViewController.stateName = [state objectAtIndex:indexPath.row];
        destViewController.pName = [num objectAtIndex:indexPath.row];
        destViewController.fName = [fax objectAtIndex:indexPath.row];
    }
}
@end

状态.h

#import <UIKit/UIKit.h>

@interface State : UIViewController

@property (nonatomic, strong) IBOutlet UILabel *stateLabel;
@property (nonatomic, strong) IBOutlet UILabel *pNumber;
@property (nonatomic, strong) IBOutlet UILabel *fNumber;
@property (nonatomic, strong) NSString *stateName;
@property (nonatomic, strong) NSString *pName;
@property (nonatomic, strong) NSString *fName;

@end

状态.m

#import "State.h"

@interface State ()

@end

@implementation State

@synthesize stateLabel;
@synthesize stateName;
@synthesize pNumber;
@synthesize fNumber;
@synthesize pName;
@synthesize fName;

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Set the Label text
    stateLabel.text = stateName;
    //number and fax in array
    pNumber.text = pName;
    fNumber.text = fName;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

我知道很多人以前遇到过这个问题,我已经尝试过他们的建议,例如确保 segues 正确连接以及清理和重建。标签仍然没有显示任何内容。我的层次结构对于 UIView 是正确的。 Here是我的 Storyboard以及状态详细信息和 StateContacts 的连接。我只是不知道还能尝试什么。

最佳答案

您的代码错误地定义了一个 tableView 属性,该属性隐藏了从 UITableView 继承的原始 tableView 属性。要解决此问题,只需删除该属性和相应的 @synthesize 指令即可。当您这样做时,您还可以删除 State 类中的 @synthesize 指令,因为编译器现在默认自动合成属性。

请注意,删除冗余 socket 后,您可能需要在 Interface Builder 中重新连接它,因此请务必仔细检查。

关于ios - 如何让标签中的文本显示出来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32289128/

相关文章:

iphone - 在 Bump Api 发送图像方面需要一些帮助

ios - 在可编码类中声明空字典

objective-c - Segue 未出现在图像选择器之后

ipad - 单点触控崩溃: Replace UINavigationController A with B and Push UiViewControllers on B

objective-c - 不同情况下带if语句的NSNumber

ios5 - 如何在 iOS 5 中将页面数组添加到 UIPageViewController?

ios - 将ScrollView添加到UIView中

ios - CLLocationManager EXC_BAD_ACCESS 错误

objective-c - 为什么在 C 中使用 floor() 时类型定义了两次?

iphone - 推送数据时返回上一个 View