Question

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
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top