iOS 7:无法连接到IBOutlets,可能是因为我使用的是UITableViewController [英] iOS 7: Cannot connect to IBOutlets, probably because I'm using UITableViewController

查看:44
本文介绍了iOS 7:无法连接到IBOutlets,可能是因为我使用的是UITableViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法创建任何IBOutlets.我正在使用tableviewcontroller而不是viewcontroller.当我单击TableViewController时,该类是UITableViewController,并且无法更改.

I cannot create any IBOutlets. I'm using a tableviewcontroller instead of a viewcontroller. When I click on TableViewController, the class is UITableViewController and I can't change that.

这是我的ViewController.h代码:

Here's my code for ViewController.h:

//  ViewController.h
//  Tips4
//
//  Created by Meghan on 1/20/14.
//  Copyright (c) 2014 Meghan. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UILabel *sliderDisplay;
@property (strong, nonatomic) IBOutlet UITextField *tempText;
@property (strong, nonatomic) IBOutlet UILabel *billTotal;
@property (nonatomic, strong) IBOutlet UISlider *slider;

- (IBAction)sliderValueChanged:(id)sender;

@property (nonatomic) float answer;
@property (nonatomic) float answerTwo;



@end

这是我的ViewController.m:

Here's my ViewController.m:

//  ViewController.m
//  Tips4
//
//  Created by Meghan on 1/20/14.
//  Copyright (c) 2014 Meghan. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tipsTableIdentifier];

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

    return cell;
}


- (IBAction)sliderValueChanged:(id)sender
{
    float theText = [_tempText.text floatValue];
    _answer = (_slider.value * theText) / 100;
    _answerTwo = _answer + theText;
    _sliderDisplay.text = [NSString stringWithFormat:@"%1.2f", _answer];
    _billTotal.text = [NSString stringWithFormat:@"%1.2f", _answerTwo];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
     _tempText.keyboardType = UIKeyboardTypeDecimalPad;

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

推荐答案

如果您使用的是TableViewController,则您的类必须继承自 UITableViewController .多数民众赞成在什么时候它会显示在身份检查器中,在那里您可以将类从 UIViewController 更改为您的类.之后,您应该可以连接IBOutlets.

If you’re using a TableViewController, your class must inherit from a UITableViewController. Thats when it will show up in the Identity Inspector, which is where you change your class from UIViewController to your class. After that you should be able to connect IBOutlets.

为此,只需替换当前行

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@interface ViewController : UITableViewController 

现在,您需要将调用超级方法的init方法添加到.m文件中.

Now, you will need to add the init method that calls the super, to the .m file.

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

我希望这样做.

有时,代码中的更改不会显示在情节提要Identity Identity Inspector中.在这种情况下,您只需退出Xcode窗口并再次打开项目.做到了.

Also, sometimes, changes in the code don’t show up in the storyboard Identity Inspector. In that case, you can just quit the Xcode window and open the project again. That does it.

或者,如果您使用ViewController,则您的类可以从 UIViewController 继承.然后,将TableView添加到View,并将UITableView实例添加到控制器文件(创建IBOutlet).在这种情况下,您的.h文件需要添加 UITableViewDelegate UITableViewDataSource 来填充表,而.m文件需要实现所需的方法(Xcode会警告您有关这个).

Alternatively, if you use a ViewController, your class can inherit from a UIViewController. Then you add a TableView to your View and add a UITableView instance to the controller file (create an IBOutlet). In this case, your .h file needs to add the UITableViewDelegate and UITableViewDataSource to populate the table and your .m file needs to implement the required methods (Xcode will warn you about this).

这篇关于iOS 7:无法连接到IBOutlets,可能是因为我使用的是UITableViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆