Error after connecting IBOutlet to UITextField - iphone

I'm getting this weird error when I connect an IBOutlet to a UITextField. It's really weird because it's only happening in this one view controller. I have two other view controllers almost identical to this one, and they function perfectly. I have a table view controller in my storyboard. It has 2 grouped sections, each with static cells. Each cell has a UITextField in it. Now, the view loads fine if I just run it without connecting the textfields to my class. However, when I do connect them, as soon as the view loads-the app crashes with this error, one for each textfield: [UITextField stringByTrimmingCharactersInSet:]: unrecognized selector sent to instance
Any idea what the cause of this could be? I'm very confused as I have other tableview controllers with the same contents and I've never gotten this error.
Here are some screenshots to help further explain my situation:
Here is the code for my .m file:
//
// IdeaViewController.m
// FinalJSApp
//
// Created by Jacob Klapper on 10/20/13.
//
//
#import "IdeaViewController.h"
#interface IdeaViewController ()
#end
#implementation IdeaViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end

You declare two properties, title and description, in your view controller that are already defined by UIViewControlller and NSObject respectively. Both of those properties as originally defined are NSStrings.
iOS is probably trying to access those properties, expecting an NSString, and getting one of your UITextFields.
Try renaming those properties and your problem should go away.

You are somehow sending stringByTrimmingCharactersInSet: to a UITextField. But stringByTrimmingCharactersInSet is an instance method of NSString and since UITextField does not implement stringByTrimmingCharactersInSet it gives you the error unrecognized selector sent to instance.

Related

How to construct a menu with a drill down table pointing to other ViewControllers in Xcode

I'm new to Objective-C, I like it,
I'm developping a free application for the local firefighters.
The app does nothing really hard, but I have a big problem with the main menu:
I have already created a table content all the section of my application, I am trying to implement the drill down method by means I can access to other ViewControllers, but I really don't know how to do it, I googled a lot but I've only found fragmentary documentation referenced to old versions of Xcode.
I'm using version 4.5.2 and the storyboard.
Here is the menu.h
#import <UIKit/UIKit.h>
#interface Menu : UITableViewController
#property (nonatomic, retain) NSMutableArray *listaMenu; //questo sarà il mio array contentente le varie opzioni della tabella
#end
And the menu.m
#import "Menu.h"
#import "News.h"
#interface Menu ()
#end
#implementation Menu
#synthesize listaMenu; //creo i metodi getter e setter
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Menu";
//Elementi da visualizzare nella tabella
listaMenu = [[NSMutableArray alloc] initWithObjects: #"News", #"Memebri", #"Calendario", #"Interventi", #"Galleria", #"Mezzi", #"Reclutamento", nil];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//funzione in cui va inserito il numero di righe da visualizzare
return [listaMenu count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell =[tableView dequeueReusableCellWithIdentifier:#"PlistCell"];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"PlistCell"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
cell.textLabel.text = [listaMenu objectAtIndex:indexPath.row];
return cell;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath //Rende gli oggetti NON editabili
{
// Return NO if you do not want the specified item to be editable.
//return YES;
return NO;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
}
#end
I hope my problem is well explained.
Thank you very much!
You need to read up about UISegue - this is the object that handles transitions from one UIViewController to another controller. In Interface Builder you can Ctrl-Drag to create a Segue between, say a UIButton and the desired UIViewController. This is the simplest usage of segues.
If you need to pass data to the destination UIViewController then you'll need to implement the - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender method. Within this method you can get the segue.destinationViewController and then send it whatever data you need. N.B. The destination view controller's view has not loaded yet, so if you're trying to customise UIViews directly, then they won't exist. You can fudge it by doing [segue.destinationViewController view] to get going, although it would be better to configure non-UI properties and have the view controller itself do any necessary customisation in viewDidLoad.

App crashes on device but not in simulator pushing an UITableView

This is the first time I write here but I have a problem that cannot handle :(...
The problem comes when I push an UITableView after push a cell in another table, if I create a general UITableViewController and push it, works, but if I redefine the class with all the necessary methods doesn´t work.
I´ve implemented this before in other code and it works, but now, after updating to Xcode 4.5, doesn´t work...
This is the source code of the view I want to push:
#interface ECDetailSettingsTableView ()
#end
#implementation ECDetailSettingsTableView
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ECDetailSettingsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"ECDetailSettingsCell"];
// Configure the cell...
return cell;
}
And this is the code that push the tableview:
/********************* Pushing View *********************/
[tableView deselectRowAtIndexPath:indexPath animated: YES];
_themeTable = [self.storyboard instantiateViewControllerWithIdentifier:#"ECDetailSettingsViewController"];
[self.navigationController pushViewController:_themeTable animated:YES];
PD: Thanks for all, and sorry if i´ve made gramatical mistakes :).
maybe if you use segue in storyboard this generete an error, because this only work with iOS 6.

why am I getting error "[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x4e38c50'"?

I made a TabBarApplication,
and made 3 controllers,
1 of them is inheriting UITableViewController , I am putting number of sections are
"return 1" and number of rows in section as "return 2",
I am getting the following problem, why?
[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x4e38c50'"
#import <UIKit/UIKit.h>
#interface List : UITableViewController {
}
#end
and .m file is
#import "List.h"
#implementation List
#pragma mark -
#pragma mark View lifecycle
/*
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
*/
/*
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 10;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
Pls check the datasource of the tableView. I think its not properly set-up. If the dataSource is not setup properly it can cause issues
This happens when you set a table view delegate/datasource that does not implement the methods declared on the delegate/datasource protocol.
Without looking at the code I cant tell you exactly what you are doing wrong but if you create the controller using an xcode template just make usre you are initializing it as a UITableViewController and nota general UIVIewController.
If you post your code I can help you work out whats wrong, but as I said, this error happens when the tableview delegate does not implement the tableView:numberOfRowsInSection: method; if you say you are implementing this method then the tableview is not assigned linked to that controller class..
Well I do this mistake all the Time. Under Outlets, make sure you drag the outlet to the "ViewController" and not the "View". I normally use the "Document Outline" for connecting just to make sure. Hope this helps.
I got the same error. I solved it by changing my View Controller's Class from UIViewController to ViewController (your view controller class ViewController.h)

Problem in Managing the reordering og rows in UITableView

I have refered "Managing the reordering of rows" in "UITableView Programming Guide".
I have written the same code for my application for rearranging the rows of tableView but not able to rearrange the rows in tableView. The delegates "canMoveRowAtIndex" and "moveRowAtIndex" have not been called though I set tableView in editing mode through "setEditing:animated".I dont want to use core data for implementing this.
Can u provide the detailed code for this?? (I would like rearrange the rows of tableView as we do for icons by long press and then moving them)
Are you sure to add properly the "Edit" button ?
On my code (withoutcoredata), with a navigation controller, I have:
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
[super setEditing:editing animated:animate];
[self.navigationItem setHidesBackButton:editing animated:YES];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
//...
}
If the methods aren't being called, chances are you either haven't set the table view instance's delegate outlet to point to your controller, or you've spelled the names of the delegate methods incorrectly.
One trick to help avoid misspellings is to go to the header file where the methods are declared (in this case, UITableViewController.h) copy the method declaration(s), and paste them into your source file. Otherwise, I try to use Xcode's completion mechanism to ensure that I don't accidentally misspell things.

TableView Does NOT Switch to Detail View When Connected to Tab Bar Controller

I have created a Tab Bar Application, removed the default FirstView and SecondView and added the following two ViewControllers:
JustAnotherView (which is simply a UIViewController with a UIView inside)
MyListOfItems (which is a UIViewController with a TableView inside)
I have a 3rd ViewController called ListOfItemsDetailViewController with a UIView inside that is supposed to be called when the user touches one of the TableView cells.
The application compiles without issue. I can touch both Tab Bar buttons and the correct View appear.
PROBLEM:
When I click a TableView cell, the code to switch to the Detail View is executed (without crashing or error), but the View is NEVER shown.
Here is a screenshot of the application and the debugger with an NSLog that shows it made it past the pushViewController without crashing:
http://clip2net.com/clip/m52549/thumb640/1281588813-9c0ba-161kb.jpg
Here is the code.
MyListOfItemsViewController.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "ListOfItemsDetailViewController.h";
#interface MyListOfItemsViewController : UIViewController {
ListOfItemsDetailViewController *listOfItemsDetailViewController;
}
#property (nonatomic, retain) ListOfItemsDetailViewController *listOfItemsDetailViewController;
#end
Here is the MyListOfItemsViewController.m file:
#import "MyListOfItemsViewController.h"
#implementation MyListOfItemsViewController
#synthesize listOfItemsDetailViewController;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.title = #"My List Of Items";
NSLog(#"My List Of Items Did Load");
}
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 5;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
NSString *cellMessage;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cellMessage = [NSString stringWithFormat:#"indexPath: %d",indexPath.row];
cell.textLabel.text = cellMessage;
return cell;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ListOfItemsDetailViewController *vcListOfItemsDetail = [[ListOfItemsDetailViewController alloc]
initWithNibName:#"ListOfItemsDetail" bundle:[NSBundle mainBundle]];
self.listOfItemsDetailViewController = vcListOfItemsDetail;
[vcListOfItemsDetail release];
[self.navigationController pushViewController:self.listOfItemsDetailViewController animated:YES];
NSLog(#"Did Execute didSelectRowAtIndexPath WITHOUT Crashing or Error.");
}
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
NSLog(#"My List Of Items View Did Unload");
}
- (void)dealloc {
[super dealloc];
}
#end
Please help. It will be very much appreciated!!!
I have been driving myself crazy trying to get basic navigation down in the iOS platform.
Also, pardon the layperson question, I know that most of you already understand how to do this.
Thanks in advance.
Jaosn
Instead of making the 2nd tab's viewcontroller to be a tableviewcontroller, make it a UINavigationController and set the navcontroller's RootViewController as "MyListOfItems". Once you do that, you will have access to the navigationController property in your viewcontroller and can use push and pop.
Or if you don't want to do this, the only way left would be to animate between the views programatically (i.e. remove one view and add the other one or add the other view on top of the current view)
In tableView:didSelectRowAtIndexPath: you are referencing a navigationController by self.navigationController, that likely doesn't exist. You have to use a UINavigationController as one view controller in you tabView and the rootViewController of that navigationController should be MyListOfItemsViewController.
Please note that you need a UINavigationController for your detail view to push on and self.navigationController is only a way to retrieve one, that has been created and inserted into the hierarchy before.