My function [ViewController.tableView reloadData] does not work, although I implemented #class ViewController in the .h file and imported #import "ViewController.h" in the .m file.
What am I doing wrong??
--- EDIT ---
Here's my .h file:
#class XMLAppDelegate, XMLProductDetailView;
#interface XMLViewController : UITableViewController <UIScrollViewDelegate, XMLImageDownloaderDelegate> {
NSMutableDictionary *imageDownloadsInProgress;
XMLAppDelegate *appDelegate;
XMLProductDetailView *productDetailView;
IBOutlet UISearchBar *searchBar;
IBOutlet UITableView *tableView;
}
#property (nonatomic, retain) NSMutableDictionary *imageDownloadsInProgress;
#property (nonatomic, retain) IBOutlet UISearchBar *searchBar;
#property (nonatomic, retain) IBOutlet UITableView *tableView;
- (void)imageDidLoad:(NSIndexPath *)indexPath;
#end
But notice: when I include IBOutlet UITableView *tableView;, it hides an instance variable.
UITableViewController already has a property named tableView. Rename your property to something like myTableView and you should be OK.
ViewController it is class name. You should init instance variable of this class
ViewController *vc = [[ViewController alloc] init];
then push it on navigation controller (if it present)
vc.myDataForTable = [NSArray .....]; //for example
[self.navigationController pushViewController:vc animated:YES];
release vc if arc is not used
[vc release];
Related
I'm building my first iOS app to try and learn iOS coding. However, I need to pass a value xAuthToken from one controller to the other. I can't seem to get it working to. I init the value in the first controller ViewController, but then I need it in SettingsController. I keep getting errors and the one it throws now is: Property 'ViewController' not found on object of type 'ViewController'
What am I doing wrong?
ViewController.h
#import <UIKit/UIKit.h>
#import "SettingsController.h"
#interface ViewController : UIViewController <UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *txtUsername;
#property (weak, nonatomic) IBOutlet UITextField *txtPassword;
- (IBAction)sigininClicked:(id)sender;
- (IBAction)backgroundTap:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic) NSString *xAuthToken;
#end
#implementation ViewController
- (IBAction)sigininClicked:(id)sender {
NSString *xAuthToken = #"0";
self.viewController = [[SettingsController alloc] init];
self.viewController.xAuthToken = xAuthToken;
[self performSegueWithIdentifier:#"login_success" sender:self];
}
SettingsController.h
#import <Foundation/Foundation.h>
#interface SettingsController : UIViewController
#end
SettingsController.m
#interface SettingsController ()
#property (weak, nonatomic) IBOutlet UITextField *myTextField;
#property (weak, nonatomic) IBOutlet UISwitch *mySwitch;
#end
#implementation SettingsController
//THIS IS WHERE I NEED XAUTHTOKEN
If you have a segue that presents your SettingsController, pass the information it needs in prepareForSegue. Don't create a new local SettingsController.
You can do something like
viewController = [self.storyboard instantiateviewcontrollerwithidentifier:#"your vc name"];
viewController.Xauthtoken = data;
// Do something else with this.
Or another way ( I don't know if it's good or bad, use it while your vc is already active ):
Use NSNotificationCenter and pass values between it.
Third way is create delegate for first Viewcontroller.
Try this in your ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic) NSString *xAuthToken;
#end
#implementation ViewController
- (IBAction)sigininClicked:(id)sender {
xAuthToken = #"Your Value Here";
[self performSegueWithIdentifier:#"login_success" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"login_success"]) {
SettingsController *destViewController = segue.destinationViewController;
destViewController.xAuthToken = xAuthToken;
}
}
And in your SettingsController.h
#import <Foundation/Foundation.h>
#interface SettingsController : UIViewController
#property (nonatomic,strong) NSString *xAuthToken;
#end
SettingsController.m
#interface SettingsController ()
#property (weak, nonatomic) IBOutlet UITextField *myTextField;
#property (weak, nonatomic) IBOutlet UISwitch *mySwitch;
#end
#implementation SettingsController
-(void)viewDidLoad{
NSLog("%#",xAuthToken);
}
Here InformacionViewController.h:
#import <UIKit/UIKit.h>
#import "LibrosFenomenales.h"
#interface InformacionViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *nombre;
#property (strong, nonatomic) IBOutlet UILabel *autor;
#property (strong, nonatomic) IBOutlet UILabel *año;
#property (strong, nonatomic) IBOutlet UILabel *genero;
#property (strong, nonatomic) IBOutlet UITextView *argumento;
#property (strong, nonatomic) IBOutlet UIImageView *portada;
#property LibrosFenomenales *libroSeleccionado;
#end
Here ViewController.m:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
InformacionViewController *informacionViewController = [self.storyboard
instantiateViewControllerWithIdentifier:#"InformacionViewController"];
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:informacionViewController];
informacionViewController.libroSeleccionado = [_libros objectAtIndex:indexPath.row];
[self presentViewController:navigationController animated:YES completion:nil];
}
Xcode shows me this error: Property "libroSeleccionado" not found on object of type "InformacionViewController".
This is the line: informacionViewController.libroSeleccionado = [_libros objectAtIndex:indexPath.row];
What am i doing wrong?
Thanks
Instead of Quiting the Xcode you just First clean the build. To clean the build Press command+Shift+K. then run the application even then If it doesn't work for then try this #property (strong, nonatomic) LibrosFenomenales *libroSeleccionado;
instead of #property LibrosFenomenales *libroSeleccionado;
Hard to say without having access to the whole code, but try these:
If "LibrosFenomenales" is an Objective-C class, change the property declaration to:
#property (strong, nonatomic) LibrosFenomenales *libroSeleccionado;
If the LibrosFenomenales.h header imports the InformacionViewController.h header, you have a cross-referencing issue. To fix this, open up InformacionViewController.h and replace:
#import "LibrosFenomenales.h"
by:
#class LibrosFenomenales;;
then open up InformacionViewController.m and add the #import "LibrosFenomenales.h" to it.
Don't forget to #import "InformacionViewController.m" in ViewController.m
Hi everyone I have been building an app and have met some problems.
My app has two viewControllers. One is MenuViewController and another one is MainViewController.
I want to pass a string from MainViewController to a mutableArray in MenuViewController, but have no idea how.
Here are my codes:
<MenuViewController.h>
#import <UIKit/UIKit.h>
#interface MenuViewController : UITableViewController {
NSMutableArray *secondFavourite;
}
#property (nonatomic, strong) NSMutableArray *secondFavourite;
#end
.
<MenuViewController.m>
#import "MenuViewController.h"
#import "MainViewController.h"
#interface MenuViewController ()
#property (strong, nonatomic) NSArray *menu;
#end
#implementation MenuViewController
#synthesize menu;
#synthesize secondFavourite;
- (void)viewDidLoad
{
[super viewDidLoad];
self.secondFavourite = [[NSMutableArray alloc] init];
self.menu = self.secondFavourite;
}
.
<MainViewController.h>
#import <UIKit/UIKit.h>
#import <social/Social.h>
#interface MainViewController : UIViewController {
IBOutlet UIImageView *imagepost;
UILabel *predictionLabel;
}
- (IBAction)sampleSelector:(UIButton *)sender;
- (IBAction)showAllClick:(id)sender;
#property (nonatomic, retain) IBOutlet UILabel *predictionLabel;
#property (strong, nonatomic) NSArray *predictionArray;
#property (strong, nonatomic) UIButton *menuBtn;
#property (strong, nonatomic) NSMutableArray *fav;
#property (strong, nonatomic) IBOutlet UILabel *favLabel;
#property (strong, nonatomic) IBOutlet UITableView* tableView;
#property (nonatomic, strong) NSMutableArray *favourite;
#end
.
<MainViewController.m>
- (void)viewDidLoad
{
[super viewDidLoad];
self.predictionArray = [[NSArray alloc] initWithObjects:#"Hey gurl", nil];
}
//Add to favourite
- (void) addToFav {
self.favourite = [[NSMutableArray alloc] init];
[self.favourite addObject:self.predictionLabel.text];
[self.tableView reloadData];
NSLog(#"%#", self.favourite);
}
//add to favourite button action
- (IBAction)addToFavButton:(id)sender {
[self addToFav];
//pass data from favourite here to secondFacourite in MenuViewController (found on stack overflow)
MenuViewController *menuViewController = [[MenuViewController alloc]initWithNibName:#"MenuViewController" bundle:nil];
menuViewController.secondFavourite = [[NSMutableArray alloc]initWithArray:self.favourite];
[self.navigationController pushViewController:menuViewController animated:YES];
}
I used NSLog to check that the menuViewController.secondFavourite in MainViewController successfully added the string into the array, isn't the array the same array in MenuViewController? Why doesn't the menu.tableView update and show the new string added? I am very confused and I hope someone will help me out.
Thanks for reading this.
This has to do with the fact that your menu viewDidLoad is overwriting the value in these two lines:
self.secondFavourite = [[NSMutableArray alloc] init];
self.menu = self.secondFavourite;
That first line is setting your secondFavourite property to an empty NSMutableArray instance. And since viewDidLoad will be called only after the view has been loaded into memory (in this case, when you try to push the view controller onto the stack), your initial values in the secondFavourite property will be lost.
Instead, you should move that initialization code into the an init method.
I am trying to take the text that is typed into UITextFields in one view, and pass those values into a different View Controller when a button is pressed. In the first view controller (CreateViewController) I have:
#import <UIKit/UIKit.h>
#import <MessageUI/MFMailComposeViewController.h>
#class ThePreviewViewController;
#interface CreateViewController : UIViewController <UIAlertViewDelegate, MFMailComposeViewControllerDelegate> {
UIDatePicker *datePicker;
UIDatePicker *datePicker2;
ThePreviewViewController *_thePreview;
}
#property (nonatomic, retain) IBOutlet UITextField *toName;
#property (nonatomic, retain) IBOutlet UITextField *fromName;
#property (nonatomic, retain) IBOutlet UITextField *issue;
#property (nonatomic, retain) ThePreviewViewController *thePreview;
#property (nonatomic, retain) IBOutlet UITextField *expire;
#property (nonatomic, retain) IBOutlet UITextField *loveMessage;
#property (nonatomic, retain) IBOutlet UITextField *gift;
-(IBAction)build;
#end
The implementation file is:
#import "ThePreviewViewController.h"
#implementation CreateViewController
#synthesize toName, fromName, gift, loveMessage, issue, expire, datePicker, datePicker2;
#synthesize thePreview = _thePreview;
-(IBAction)preview {
if (_thePreview == nil) {
self.thePreview = [[ThePreviewViewController alloc] initWithNibName:#"ThePreviewViewController" bundle:[NSBundle mainBundle]] ;
}
_thePreview.issue.text = issue.text;
_thePreview.gift.text = gift.text;
_thePreview.expire.text = expire.text;
[self.navigationController pushViewController:_thePreview animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
However, after I connect all the outlets, the labels in ThePreviewViewController stay at null. Thoughts?
It looks like this is due to an issue which has been discussed here before: when you instantiate your ThePreviewViewController its UIKit instances are not yet available to have their properties set.
The solution is to create and set NSString properties on your ThePreviewViewController (or more compactly use an NSDictionary) which then use, in its ViewWillAppear event handler to set the properties of its UIKit elements.
Instead of setting UITextField text in ThePreview VC, you should set a NSString property. This is because the UITextField might not have been loaded yet and _thePreview.issue.text = issue.text, will have no effect, since _thePreview.issue has still not been created. So create a property (eg. NSString *issueString) in your ThePreview VC and then set that property as _thePreview.issueString = issue.text in your CreateViewController, and then finally in your ThePreview VC set your textField text in your viewDidLoad method using self.issue.text = self.issueString;
Hope this helps.
I was working on my iOS app last night when I went to test it and it crashed on startup. I wasn't even working on the nib that is causing the crash. Anyways, here is the error code:
2/29/12 10:32:05.291 AM Safe Flight: *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIApplication 0xdd496f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Airport1.'
*** First throw call stack:
(0x2873052 0x33add0a 0x2872f11 0x1ae2032 0x1a53f7b 0x1a53eeb 0x1a6ed60 0x136191a 0x2874e1a 0x27de821 0x136046e 0x1362010 0x114214a 0x1142461 0x11417c0 0x1150743 0x11511f8 0x1144aa9 0x37b7fa9 0x28471c5 0x27ac022 0x27aa90a 0x27a9db4 0x27a9ccb 0x11412a7 0x1142a9b 0x3710 0x2f35)
Airport1 is just a label with an IBOutlet correctly connected to it. Also, if I delete the connection the error just is with the next item that has an connection.
Also, my FileOwners class it correctly set to the appropriate viewController.
// MasterViewController.h
#import <UIKit/UIKit.h>
#import "MobclixAds.h"
#class DataViewController;
#class EditViewController;
#class SearchViewController;
#interface MasterViewController : UIViewController{
UILabel *Airport1;
UILabel *Airport2;
UILabel *Airport3;
UILabel *Airport4;
UILabel *Airport5;
UILabel *Airport6;
UILabel *Airport7;
UILabel *Airport8;
UILabel *Airport9;
UIButton *Airport1B;
UIButton *Airport2B;
UIButton *Airport3B;
UIButton *Airport4B;
UIButton *Airport5B;
UIButton *Airport6B;
UIButton *Airport7B;
UIButton *Airport8B;
UIButton *Airport9B;
MobclixAdView* adView;
}
#property (strong, nonatomic) DataViewController *dataViewController;
#property (strong, nonatomic) EditViewController *editViewController;
#property (strong, nonatomic) SearchViewController *searchViewController;
#property (nonatomic, retain) IBOutlet UILabel *Airport1;
#property (nonatomic, retain) IBOutlet UILabel *Airport2;
#property (nonatomic, retain) IBOutlet UILabel *Airport3;
#property (nonatomic, retain) IBOutlet UILabel *Airport4;
#property (nonatomic, retain) IBOutlet UILabel *Airport5;
#property (nonatomic, retain) IBOutlet UILabel *Airport6;
#property (nonatomic, retain) IBOutlet UILabel *Airport7;
#property (nonatomic, retain) IBOutlet UILabel *Airport8;
#property (nonatomic, retain) IBOutlet UILabel *Airport9;
#property (nonatomic, retain) IBOutlet UIButton *Airport1B;
#property (nonatomic, retain) IBOutlet UIButton *Airport2B;
#property (nonatomic, retain) IBOutlet UIButton *Airport3B;
#property (nonatomic, retain) IBOutlet UIButton *Airport4B;
#property (nonatomic, retain) IBOutlet UIButton *Airport5B;
#property (nonatomic, retain) IBOutlet UIButton *Airport6B;
#property (nonatomic, retain) IBOutlet UIButton *Airport7B;
#property (nonatomic, retain) IBOutlet UIButton *Airport8B;
#property (nonatomic, retain) IBOutlet UIButton *Airport9B;
#property(nonatomic,retain) IBOutlet MobclixAdView* adView;
#end
and
// MasterViewController.m
#import "MasterViewController.h"
#import "DataViewController.h"
#import "EditViewController.h"
#import "SearchViewController.h"
#implementation MasterViewController
#synthesize dataViewController = _dataViewController;
#synthesize editViewController = _editViewController;
#synthesize searchViewController = _searchViewController;
#synthesize Airport1, Airport2, Airport3, Airport4, Airport5, Airport6, Airport7, Airport8, Airport9;
#synthesize Airport1B, Airport2B, Airport3B, Airport4B, Airport5B, Airport6B, Airport7B, Airport8B, Airport9B;
#synthesize adView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Safe Flight";
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
}
return self;
}
So is my problem with my code? or with Xcode itself?? I have restarted Xcode and the simulator and even did a "clean" build (it sounded like it would help...)
Thanks,
Andrew
EDIT This is my AppDelegate and I'm setting the correct xib to the window
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[Mobclix start];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
PROBLEM SOLVED!!!
I can't answer my own question because of some stupid spam filter... and I can post a picture of the problem... but its fixed!
but here is a link to the problem click here
Found the Problem!! I feel kinda stupid now ;) but anyways, here is a screenshot of the problem!
The reason causing to crash could be the connection mistakes within xib to header.
Airport1 could be connected to another class than MasterViewController.
If your label connected to File's Owner - it should be MasterViewController.
If you've created another UIViewController within xib and connected Airport1 label to it, then this controller also should be connected to MasterViewController (Identity Inspector "Class" field.)
Updated. Added Screenshots:
When you initialize the MasterViewController (wherever you are launching it from), make sure you are initializing it with the appropriate nib file
Ex.
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:#"YourNibName" bundle:nil];