UIApplication sharedApplication Help? - objective-c

I'm trying to access a variable that creates a navigation controller that I defined in my App Delegate in a view controller (I'm building a tab bar application with a .plist for each tab, like a drill-down app). I'm trying to get rid of the error that says "request for member "indNavControl" in something not a structure or union. Could somebody help me out?
Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
A37dgAppDelegate *AppDelegate = (A37dgAppDelegate *)[[UIApplication sharedApplication] delegate];
self.indNavControl = [AppDelegate.indNavControl];
//Get the dictionary of the selected data source.
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
//Get the children of the present item.
NSArray *Children = [dictionary objectForKey:#"Children"];
if([Children count] == 0) {
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:[NSBundle mainBundle]];
A37dgAppDelegate *AppDelegate = (A37dgAppDelegate *)[[UIApplication sharedApplication] delegate];
[AppDelegate.indNavControll push indNavControl];
[self.indNavControl pushViewController:dvController animated:YES];
[dvController release];
}
else {
//Prepare to tableview.
IndustriesViewController *indViewControl = [[IndustriesViewController alloc] initWithNibName:#"IndustryView" bundle:[NSBundle mainBundle]];
//Increment the Current View
indViewControl.CurrentLevel += 1;
//Set the title;
indViewControl.CurrentTitle = [dictionary objectForKey:#"Title"];
//Push the new table view on the stack
A37dgAppDelegate *AppDelegate = (A37dgAppDelegate *)[[UIApplication sharedApplication] delegate];
[self.indNavControl pushViewController:indViewControl animated:YES];
indViewControl.tableDataSource = Children;
[indViewControl release];
}
}
Really, all I want to do is reference my view controller using the UIApplication sharedApplication method, but I can't figure out how.

Did you import "A37dgAppDelegate.h" and "IndustriesViewController.h" here?

Did you declare 'indNavControl' in your AppDelegate as property? There should be
#property (nonatomic, retain) NSObject *indNavController;
or something like that in your AppDelegate.h file. Check if you have it. If not, add it.
EDIT: Also, since there is no ObjC method in
[AppDelegate.indNavControl];
you should get rid of [ and ] in this line.
EDIT2: You have multiple instances of your app delegate: at the beginning and inside 'if' statement. And their names are the same. You should delete
A37dgAppDelegate *AppDelegate = ...
inside "if" statement, because variable named "AppDelegate" already exists.

Which line? YOu have a typo (AppDelegate.indNavControll).
And it looks like you're using the horrible practice (yeah, I know Apple does it) of naming instance variables and properties the same, which makes things harder to read out of context. Is indNavControl a property of another class too? And an instance variable of that class too?

Related

How to implement didSelectRowAtIndexPath method with sectioned UITableView

I have been implementing a app with sectioned UITableView. In the tableview, I used a array, called sections, to store different classes array and show them in different sections. On the other hand, I also used a array, called headers, to store section headers' name. For example, in the "weekday" section, there are seven words, Sunday to Saturday, in the tableViewCell under this section. In the MainViewController:
-(void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *weekday=[[NSMutableArray alloc]init];
NSMutableArray *traffic=[[NSMutableArray alloc]init];
for (NSArray *sec_ary in listData) {
NSString *class=[[sec_ary valueForKey:#"vocabulary_list"]valueForKey:#"Class"];
if ([class isEqualToString:#"WEEKDAY"]) {
[weekday addObject:[[sec_ary valueForKey:#"vocabulary_list"]valueForKey:#"Vocabulary"]];
}else if ([class isEqualToString:#"TRAFFIC TOOLS"]){
[traffic addObject:[[sec_ary valueForKey:#"vocabulary_list"]valueForKey:#"Vocabulary"]];
}
sections=[[NSArray alloc ]initWithObjects:weekday,traffic,nil];
headers=[[NSArray alloc]initWithObjects:#"WEEKDAY",#"TRAFFIC TOOLS",nil];
}
In this App, I also implemented the navigation controller, which can bring user to view detail information in the didSelectRowAtIndexPath method. The codes are following
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
ContentViewController *content= [[ContentViewController alloc] initwithIndexPath:indexPath];
[delegate.navController1 pushViewController:content animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *TableIdentifier = #"tableidentifier"; /
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:TableIdentifier] autorelease];
}
cell.textLabel.text = (NSString*)[[self.sections objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
return cell;
}
In the ContentViewController:
-(void)viewDidLoad{
[super viewDidLoad];
AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
NSDictionary *voc_list=[delegate.vcblr objectAtIndex:index.row];
//extracting the voc_list dictionary to show infomation.....
}
I am wondering why each section will load the information from beginning in the voc_list. That is to say, the tableViewCell do not respond to the right detail content. Could anyone provide me with ideas to resolve it?
I am wondering why each section will load the information from beginning in the voc_list.
Because the code of your viewDidLoad method ignores index.section, paying attention to index.row alone. You need to decide on the index that you pass to delegate.vcblr based on both the section and the row; the exact calculation depends on the structure of your data, i.e. how many rows you have in each section.
Note : Using AppDelegate to share data among view controllers is not a good idea. Consider separating out your data in a separate model class, and make that class a singleton.

Push a string and display in a UITextField

I am doing a small app and I am using a UISplitVIewController and I am trying to push the UITable cell text to the detail view and display it in a UITextField. I have managed to push the string nicely when i test it with a NSLog but when i apply to the UITextField it does not display not sure why here my method: (I am doing this using a storyboard)
-(void)pushModuleName:(NSString*)moduleName
{
self.Lv4ModuleTitleTextField.text = moduleName;
NSLog(#"name pushed%#",moduleName);
}
Not sure why this doesn't work.
[UPDATE]
UITableViewController.m (where the method is called)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selectedModule = [NSString stringWithFormat:#"%#",[_numberOfRows objectAtIndex:indexPath.row]];
Lv4GradeViewController *lv4 = [[Lv4GradeViewController alloc] initWithNibName:nil bundle:nil];
[lv4 pushModuleName:selectedModule];
}
You are creating a new instance of Lv4GradeViewController with the following code but no XIB file which will contain the text field:
Lv4GradeViewController *lv4 = [[Lv4GradeViewController alloc] initWithNibName:nil bundle:nil];
I would suggest you instantiate Lv4GradeViewController with the storyboard instantiateViewControllerWithIdentifier method:
Lv4GradeViewController *lv4 = [self.storyboard instantiateViewControllerWithIdentifier:#"IdentifierName"];
And remember to set the storyboard ID of the Lv4GradeViewController the same as IdentifierName in the storyboard.
The problem is that you're not getting a reference to the Lv4GradeViewController that you have on screen, you're creating a new one with alloc init. A split view controller, has a viewControllers array, with the controller at index 0 being the master controller, and the one at index 1 being the detail controller. So, the didSelectRowAtIndexPath method should look like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *selectedModule = [NSString stringWithFormat:#"%#",[_numberOfRows objectAtIndex:indexPath.row]];
Lv4GradeViewController *lv4 = self.splitViewController.viewControllers[1];
[lv4 pushModuleName:selectedModule];
}

How to get the user's choice properly when the choice is too complex to use UIAlertView

I have been struggling with this problem for a while now, so any help would be greatly appreciated.
Here is the situation: My application has a UIViewController subclass called InitialViewController. This view controller has a UIButton, and when that button is pressed it creates a NSObject subclass called MyEngine. Something like this:
#interface InitialViewController : UIViewController <MyEngineDelegate>
...
#end
#implementation InitialViewController
...
-(IBAction)pressedButton:(id)sender {
MyEngine *engine = [[MyEngine alloc] init];
[engine start];
}
Inside start, I present a ViewController (ConflictViewController) modally to get the user's choice:
#interface MyEngine : NSObject <ConflictViewControllerDelegate>
...
-(void) start;
#end
#implementation MyEngine
...
-(void) start {
ConflictViewcontroller *cvc = [[ConflictViewController alloc] initWithNibName:#"ConflictViewController" bundle:nil];
cvc.modalPresentationStyle = UIModalPresentationFormSheet;
cvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
cvc.delegate = self;
UIWindow *window = [(MyAppDelegate *) [[UIApplication sharedApplication] delegate] window];
[[window rootViewController] presentModalViewController:cvc animated:YES];
}
#end
ConflictViewController is really simple. It just waits for the user to decide, and when the user press the button, it send the message to the delegate, and dismiss itself.
-(IBAction)didSelectConflict:(id)sender {
UISegmentedControl *seg = (UISegmentedControl*) sender;
[self.delegate didResolveConflictChoice:seg.selectedSegmentIndex];
[self dismissModalViewControllerAnimated:YES];
}
I've checked every connection, all the delegates are working properly.
What is going wrong is:
When MyEngine receives the user's choice in it's implementation of didSelectConflict: it cannot continue properly because all of it's properties have gone null.
When the MyEngine presents the ConflictViewController, the program continues the execution and when start finishes, it goes back to pressedButton: and when this method is closed, the MyEngine object gets released.
What i want to know is if there is way around this ? Has anyone done something like this in another way ?
The question here is: How to get the user's choice properly when the choice is too complex to use UIAlertView.
Sorry for the long question, I simplified it as much as I could. Thanks for your time, any links, comments, or any kind of help is greatly appreciated
Why are you initializing MyEngine *engine in the IBAction, if you wish to use a MyEngine object why don't you make a global declaration in your InitialViewController and just call [engine start] in the IBaction. Then when the delegate method returns the selected index you can apply that to a global int in your initial view controller and continue on your way. Hope that makes sense
Make your method start as
-(void) startWithDelegate:(id)del {
ConflictViewcontroller *cvc = [[ConflictViewController alloc] initWithNibName:#"ConflictViewController" bundle:nil];
cvc.modalPresentationStyle = UIModalPresentationFormSheet;
cvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
cvc.delegate = del;
UIWindow *window = [(MyAppDelegate *) [[UIApplication sharedApplication] delegate] window];
[[window rootViewController] presentModalViewController:cvc animated:YES];
}
and
-(IBAction)pressedButton:(id)sender {
MyEngine *engine = [[MyEngine alloc] init];
[engine startWithDelegate:self];
}
implement didResolveConflictChoice: in InitialViewController and get the delegate call there.
OR you can use UIActionSheet if suitable.

How to push next view with TTNavigator?

I'm using a modified View-Based Application, where I have starting UIViewController showing a input control & a TTThumbsViewController showing the results. I'm passing them in the AppDelegate using TTNavigator initWithName: for the TTThumbsViewController.
I did this by myself reading from the documentation:
The ViewDidLoad method inside my TTThumbsViewController:
- (void)viewDidLoad {
self.photoSource = [[PhotoSource alloc]
initWithType:PhotoSourceNormal
title:myTitle
photos:myImages
photos2:nil
];
}
The implementation of my AppDelegate:
#implementation geoFlickrAppDelegate
#synthesize window=_window;
#synthesize viewController=_viewController;
#synthesize navigator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
navigator =[TTNavigator navigator];
navigator.window = self.window;
TTURLMap *map = navigator.URLMap;
[map from:#"app://home/" toViewController:[geoFlickrViewController class]];
[map from:#"app://album/(initWithName:)" toViewController:[AlbumController class]];
[navigator openURLAction:[TTURLAction actionWithURLPath:#"app://home/"]];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
-(void)toGallery:(NSString*)txt
{
[navigator openURLAction:[TTURLAction actionWithURLPath:txt]];
}
The event inside my UIViewController for pushing the next view:
-(IBAction)search:(id)sender
{
NSString *str = [[NSString alloc] initWithFormat:#"app://album/%#",txtSearch.text];
geoFlickrAppDelegate *appDelegate = (geoFlickrAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate toGallery:str];
}
The result from this code is that the input is passed through my AppDelegate in the TTThumbsViewController using initWithName: but the never gets pushed in & my ViewDidLoad method never gets called. Any idea why is that so?
Whenever I had an error like this, it was the initializer (in your case initWithName:) returning nil. This is the first thing I would check. If that doesn't solve try setting a breakpoint in [TTBaseNavigator presentController:parentURLPath:withPattern:action:].
If that method is not reached something is wrong with your URL-Map. You may for Example need to urlencode your users input. URL-based Navigation works with URLs. Strings that can not be used in URLs can not be passed unencoded.
If that method is reached, you may use the debugger and step through the code from there to find out whats wrong.
Another thing I would like to mention, is that you really do not need to reference your appDelegate with [[UIApplication sharedApplication] delegate] when you like to use the navigator. This is one of the reasons why the navigator is so useful. Try to change the call to:
-(IBAction)search:(id)sender
{
NSString *str = [[NSString alloc] initWithFormat:#"app://album/%#",txtSearch.text];
TTOpenURLFromView(str, self.view);
TTOpenURL(str); // Three20 < 1.0.3
}
You can then get rid of the toGallery: method.
In my code I have no:
navigator.window = self.window;
and instantiate the window calling:
navigator =[TTNavigator navigator];
navigator.window;
and also change [self.window makeKeyAndVisible] into
[navigator.window makeKeyAndVisible]

Passing variables (or similar) to newly loaded view

I am loading new views for a small iphone app, and was wondering how to pass details from one to another?
I am loading a tableview full of data from and xml file, then once clicked a new view is brought in via:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SubInfoViewController *subcontroller = [[SubInfoViewController alloc] initWithNibName:#"SubInfoView" bundle:nil];
[self presentModalViewController:subcontroller animated:YES];
[subcontroller release];
}
Next step would be to tell the newly loaded view which row had just been loaded?
Any idea, thoughts more than welcome, and please be gentle big newbie...
I typically create my own init method to do things like this. I think it would likely be better to pass in the corresponding "model" object represented by the tableView row, rather than the row number itself, like this:
In SubInfoViewController.h
#interface SubInfoViewController : UIViewController {
YourObject *yourObject;
}
#property (nonatomic, retain) YourObject *yourObject;
Then in SubInfoViewController.m:
- (SubInfoViewController*)initWithYourObject:(YourObject*)anObject {
if((self = [super initWithNibName#"SubInfoView" bundle:nil])) {
self.yourObject = anObject;
}
return self;
}
You'd create and present it this way:
// assuming you've got an array storing objects represented
// in the tableView called objectArray
SubInfoViewController *vc = [[SubInfoViewController alloc] initWithYourObject:[objectArray objectAtIndex:indexPath.row]];
[self presentModalViewController:vc animated:YES];
[vc release];
This could be adapted pretty easily to allow you to pass in any type of object or value (such as a row number if you still want to do that).
Add an instance variable to your view controller and declare a property corresponding to it, so after you alloc, init it, set it like subcontroller.foo = Blah Blah.