Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Playlist Browser with Objective-C Working With Multiple View Controllers Modifying the UI

Shaun Kelly
Shaun Kelly
5,648 Points

App crashes as soon as i press the button...

PlayListMasterViewController.h
@interface PlayListMasterViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *aButton;

@end
PlayListMasterViewController.m
#import "PlayListMasterViewController.h"
#import "PlayListDetailViewController.h"

@interface PlayListMasterViewController ()

@end

@implementation PlayListMasterViewController

- (void)viewDidLoad {
    [super viewDidLoad];



    [self.aButton setTitle:@"Press me!" forState:UIControlStateNormal];



}

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



-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqual:@"showPlayListDetail"]) {

        PlayListDetailViewController *playListDetailController = (PlayListDetailViewController *)segue.destinationViewController;

        playListDetailController.segueLabelText = @"Yay! you pressed the button";

    }



}
@end
PlayListDetailViewController.h
@interface PlayListDetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *buttonPressLabel;

@property (strong, nonatomic) NSString *segueLabelText;
@end
PlayListDetailViewController.m
#import "PlayListDetailViewController.h"

@interface PlayListDetailViewController ()

@end

@implementation PlayListDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];



    self.buttonPressLabel.text = self.segueLabelText;

}

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



@end
Chris Howie
Chris Howie
13,216 Points

Shaun not sure if you figure this out, but I seem to have had the same problem. I was able to figure out it was the referencing outlets. The interesting thing is that it was related to aButton on the playlist main page.

To correct it, I selected Button, then I opened the connections inspector. I noticed that somehow sent events were selected for this button. Remove those, then we need to setup the referencing outlets.

Click and drag from the referencing outlets + option to the button. Then choose aButton from the pop up menu.

This will connect the button to the referencing outlet.

This happened to me twice. Let me know what you did to resolve it.

This resolved the issue for me.

4 Answers

Shaun, I don't see a problem from the code that you have posted but I have spotted some potential ones. Could you possibly post the error in the log area when the app crashes? Knowing this will help me help you.

If you have any questions, feel free to ask!

Shaun Kelly
Shaun Kelly
5,648 Points

This is what it says in the log......

2015-04-03 09:01:03.467 PlayList Browser[1216:245291] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<PlayListDetailViewController 0x15e43a20> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key buttonPressedLabel.' *** First throw call stack: (0x2290e45f 0x307a4c8b 0x2290e169 0x23599a59 0x235abcaf 0x2285e85d 0x261e9ad1 0x26108077 0x25f46e99 0x25e2b0ed 0x25e2b071 0x25fb4445 0x25ed4027 0x25ed3e2d 0x25ed3dc1 0x25e287ff 0x2584e835 0x2584a20d 0x2584a095 0x25849a71 0x25849875 0x25e2a203 0x228d4fbf 0x228d43cf 0x228d2a35 0x228203b1 0x228201c3 0x29e4d201 0x25e8a43d 0x4ed39 0x30d30aaf) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

Shaun, your code seems correct. After reviewing, I have spotted another potential problem. In the implementation, you set the value of the label to the property on the class, simple. The only potential problem I can see is you may have not connected the outlet within the editor. Even if you have, changing the name of a class, accidentally recreating the view controller from scratch without reassigning the controller owner in the identity inspector, or even without changing anything at all in some cases can cause this bug within Xcode.

Do you know how to see if the label is connected to Interface Builder?

If you have any questions, feel free to ask!

Shaun Kelly
Shaun Kelly
5,648 Points

I don't know what went wrong but i recreated the label and linked it back up. So need to know what went wrong?

Amila de Saram
Amila de Saram
2,090 Points

Hi, I have the same problem--the app crashes after I press the button on the Playlists screen and never gets to the Artists screen.

Here's my code:

import UIKit

class PlaylistMasterViewController: UIViewController {

@IBOutlet weak var aButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    aButton.setTitle("Press me!", forState: .Normal)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
     if segue.identifier == "showPlaylistDetail" {
        let playlistDetailController = segue.destinationViewController as
        PlaylistDetailViewController
        playlistDetailController.segueLabelText = "Yay! You pressed the button!"

}

}


import UIKit

class PlaylistDetailViewController: UIViewController {

@IBOutlet weak var buttonPressLabel: UILabel!
var segueLabelText: String = ""

override func viewDidLoad() {
    super.viewDidLoad()

    buttonPressLabel.text = segueLabelText


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}


And the error (copied and pasted from the debug area):

segue UIStoryboardSegue 0x00007fd099502990 0x00007fd099502990 sender AnyObject? (instance_type = Builtin.RawPointer = 0x00007fd09958bdf0 -> 0x0000000103051b28 (void *)0x0000000103051ba0: UIButton) Some self AnyaTunes.PlaylistMasterViewController 0x00007fd0995720d0 0x00007fd0995720d0 playlistDetailController AnyaTunes.PlaylistDetailViewController

Shaun Kelly
Shaun Kelly
5,648 Points

Is this code in swift ?

Amila de Saram
Amila de Saram
2,090 Points

The code is in swift and I figured out what the problem was--I had added a breakpoint by mistake. Duh! Thanks for your help though.