iOS 7: Cannot connect to IBOutlets, probably because I'm using UITableViewController

独自空忆成欢 提交于 2019-12-13 17:58:09

问题


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.

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

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

回答1:


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.

To do that, just replace your current line

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

with

@interface ViewController : UITableViewController 

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;
}

I hope this will do it.

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.

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).



来源:https://stackoverflow.com/questions/21247728/ios-7-cannot-connect-to-iboutlets-probably-because-im-using-uitableviewcontro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!