iPhone Reading Property From Other Class In UITab - iphone

I'm new to Objective-C and have a little bit of problem. I made an app that has a button that switches to tab number 2 in the app but I want it to get specific data from the database so I've written code in which I pass from the first view a number which is the id of the row I need to the second view
#synthesize goTo;
-(IBAction)goToChapter:(id)sender{
self.goTo= (int)10 ;
self.tabBarController.selectedViewController
= [self.tabBarController.viewControllers objectAtIndex:1];
}
So in the second class i tried this:
- (void)viewDidLoad {
[super viewDidLoad];
FirstViewController *fvc=[[FirstViewController alloc] init];
int inx = (int)fvc.goTo ;
[self getPageContent:inx];
}
but what I got is that the goTo is with a zero not 10 as suppose to be.
What am I doing wrong and how can this be handled?
Thanks in advance :)
Emad Hegab

Try this:
- (void)viewDidLoad {
[super viewDidLoad];
FirstViewController *fvc = (FirstViewController *)[self.tabBarController.viewControllers objectAtIndex:0];
int inx = fvc.goTo;
[self getPageContent:inx];
}
This assumes your FirstViewController is at index 0.
You were creating a new instance of FirstViewController instead of getting a reference to the already existing instance.
You also don't need to do the (int) casts.
Also, the viewDidLoad will probably only run the first time you go to the second tab. So when the user tabs back to the first VC and picks another chapter, the viewDidLoad will not execute. You might want to use viewDidAppear instead.

Related

InitWithName Execution Before ViewDidLoad Possible?

I'm trying to move an NSString between two View Controllers and after searching around all convoluted ways, the easiest and most straight-forward way I want to get used to was to write an initWithName function in the Receiving VC and calling it in the Sending VC. It does move it successfully but I want it to get executed before ViewDidLoad loads the textViewer so that it shows right after the tab button is pressed. Here's the code from the sending VC:
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString: #""]) {
textView.text = #"*Paste the machine code in question here*";
}
SecondViewController *theVCMover = [[SecondViewController alloc] initWithName: textView.text];
[self.navigationController pushViewController:theVCMover animated:YES]; //Is this really necessary if I'm not going to segue it directly, I'm just waiting for the user to press the next tab
gotItLabel.text = #"Got it! Ready for action...";
}
And here's the code on the receiving VC:
- (id)initWithName:(NSString *)theVCMovee {
self = [super initWithNibName:#"SecondViewController" bundle:nil];
if (self) {
rawUserInput = theVCMovee;
CleanerText.text = rawUserInput;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
CleanerText.text = rawUserInput;
NSLog(#"Got the other tab's text and it's %# ", rawUserInput);
}
Your code is mostly fine, but you'll find that as you have more complex view controllers that you won't necessarily want to write custom initializers to do every bit of property setting. Note that if CleanerText is a UI element that you're loading from your nib, it doesn't help to set CleanerText.text in your init method—it's not loaded until -viewDidLoad is called.
You don't have to do everything in init, though, if you declare properties for rawUserInput or other variables you want to set. You can then just go:
SecondViewController *theVCMover = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
theVCMover.rawUserInput = textView.text;
theVCMover.otherProperty = otherValue;
....
And the rest of your code works the same.
You can't (reliably) call methods on an instance until init has finished executing, so this pattern is "safe" and is how it's supposed to work.

Handling string objects and View Controllers

I am trying to pass a string back and forth between the view Controllers, so for example as soon as I click on a tab bar button (+) in the first View, second view opens (PresentModalViewController) and it has a Text Field. So anything I type, I take it into a string(this string is an object of the first view) and I am trying to append that string to a tableview loaded in the first View.
Note: My string object is declared like this
View1.h
NSString *string
#property (copy) NSString *string;
View1.m
#synthesize string;
And in the View 2 I am passing the textField Value like this
View2.m
View1 *view1 = [[View1 alloc] initWithNibName:#"View1" bundle:nil];
view1.string = [[NSString alloc] initWithFormat:#"%#", TextField.text];
Problem - When I NSLog this value inside the View2, it grabs the value from the Text Field but in order for me to load the previous view, I need to dismiss this View2. So as soon as this View2 is dismissed when I try to access the same string object in my view 1. It says the string object is null.
Question - Could someone tell me
1. How to get the text Field value from view 2 to view 1 after dismissing View 2 (does it really makes all its objects null when dismissed?)
2. How to append that string to the last index of a NSMutableArray?
This is a very good question that I also had trouble figuring out when I started coding for the iOS. Basically, you don't need to initialize a new view1 because the tabbar controller already holds the view1 object in its viewControllers property. Also, alloc/init'ing the string in not necessary in this situation.
Therefore, you would want to change this:
View1 *view1 = [[View1 alloc] initWithNibName:#"View1" bundle:nil];
view1.string = [[NSString alloc] initWithFormat:#"%#", TextField.text];
To something like this:
View1 *view1 = [self.tabbarController.viewControllers objectAtIndex:0];
view1.string = textField.text;
Or even:
((View1 *)[self.tabbarController.viewControllers objectAtIndex:0]).string = textField.text;
Part 2:
How to append that string to the last index of a NSMutableArray?
NSMutableArray *someArray = [[NSMutableArray alloc] init];
[someArray addObject:string];
[someArray addObject:#"anotherString"];
The answer from #chown will definitely work if the ViewController you're sending the string to is the base controller of a tabBarController.
If you were several levels deep into a NavigationController stack, then you'll need a different approach.
The approach I'd recommend would be to create a protocol. This is where you create a delegate of view2 to pass the string back down the stack before the view is dismissed.
There are loads of examples of this code both in the Apple Documentation and on the Internet (StackOverflow included) but here's a quick run down...
In View2.h
#import <UIKit/UIKit.h>
//define the protocol, so you can set the delegate to this type
#protocol View2Delegate;
#interface View2 : UIViewController
//other properties etc
#property (assign) id <View2Delegate> delegate;
#end
//put the actual protocol definition here so we can pass a reference to ourself back up too if needed...
#protocol View2Delegate
- (void)view2:(View2*)view passingStringBack:(NSString *)stringToPassBack;
#end
In View2.m you can call that delegate method where ever you like but here's an example:
- (void)viewWillDisappear:(BOOL)animated
{
if(self.delegate)
[self.delegate view2:self passingStringBack:#"String I'm passing back"];
[super viewWillDisappear:animated];
}
Then in View1.h
#interface View2 : UIViewController <View2Delegate>
and View1.m
- (void)view2:(View2*)view passingStringBack:(NSString *)stringToPassBack
{
NSLog(#"%#", stringToPassBack);
}
Another option would be to post a notification, but that is more a broadcast scenario than a targeted message so I won't bother posting example code for that.

passing value in init method

I am a new programmer of Iphone Application. I work dynamically not use nib files.
I want to pass values from one view controller to another.
in firstView.m
these two values
cell.textLabel.text
cell.detailTextLabel.text
i want to pass in the TabBarSearchThirdView init method..
TabBarSearchThirdView *thirdView=[[TabBarSearchThirdView alloc]init];
[myView addSubview:thirdView.view];
in TabBarSearchThirdView.m file.
-(id)init
{
firstVariable=..........
secondVariable=..........
}
Hope you understood my question. Give me little code or suggestions if possible
and give me a link of easy and good table view tutorial. If possible
Thanks in advance
You should declare a new init method like this one
- (id)initWithText:(NSString *)aText andDetail:(NSString *)aDetail{
self = [super init];
if(self){
firstVariable = aText;
secondVariable = aDetail;
}
return self;
}
For passing data between viewcontrollers my answer at the below link will help.
How to pass a tableview cell value to another page(uiviewcontroller)?
for passing values from 1 controller to another controller, you have to define 2 variables in AppDelegate class, with property and synthesis . example NSstring *firstVariable and NSString *secondVariable and mention its property and synthesis.
and after your code
TabBarSearchThirdView *thirdView=[[TabBarSearchThirdView alloc]init];
MyAppnameDelegate AppDel = (MyAppnameAppDelegate*)[[UIApplication sharedApplication]delegate];
//MyAppnameDelegate is your appdelegete class, so change it by your appDelegate class
AppDel.firstVariable =cell.textLabel.text;
AppDel. secondVariable = cell.detailTextLabel.text;
[myView addSubview:thirdView.view];
process to access values in Third class is
In viewDidLoad{
MyAppnameDelegate AppDel = (MyAppnameAppDelegate*)[[UIApplication sharedApplication]delegate];
//MyAppnameDelegate is your appdelegete class, so change it by your appDelegate class
nsstring *accessfirstVariable =.AppDel.firstVariable
nsstring *accesssecondVariable =AppDel. secondVariable
}
and use this where ever u like… cheers

Error When Using UIApplication sharedApplication?

Obviously, I'm new to iOS development, but I could really use somebody's help. I am building a tab bar application in which I am attempting to load .plists into drill-down table views. The problem is that I can't seem to get this method right, as I am trying to use the navigation controller for a tab in its ViewController. I'm positive that my error is in the second line.
A37dgAppDelegate *AppDelegate = (A37dgAppDelegate *)[[UIApplication sharedApplication] delegate];
AppDelegate.indNavControl *indNavControl;
Subsequently, I get some errors. Here is the code, and I'll point out where the errors are:
if([Children count] == 0) {
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:[NSBundle mainBundle]];
[self.indNavControl pushViewController:dvController animated:YES];//Property "indNavControl" not found on object of type "IndustriesViewController"
[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
[self.indNavControl pushViewController:indViewControl animated:YES]; //Property "indNavControl" not found on object of type "IndustriesViewController"
indViewControl.tableDataSource = Children;
[indViewControl release];
}
}
Just to be clear, I have imported the header file of my App Delegate. Any help would be greatly appreciated :)
The line AppDelegate.indNavControl *indNavControl; is wrong. I think you don't need this line at all. And while pushing new view controller you can directly use AppDelegate.indNavControl instead of self.indNavControl,
[AppDelegate.indNavControl pushViewController:...
Your theory that the problem is in the second line is correct.
AppDelegate.indNavControl *indNavControl;
that line isn't really doing anything. You need a class where AppDelegate.indNavControl is.
You have a couple of options:
1) if you want indViewControl to be a 'public' property
* in your IndustriesViewController.h, move the declaration of indViewControl there.
#interface
{
WhateverClass *indNavControl;
}
#property (retain) WhateverClass*indNavControl;
#end
2) if you want a private property then add an empty category at the top of your .m with the ivar and property declaration from above.
What the compiler is complaining about is trying to access a property that doesn't exist.
calling self.whateverIvar requires an #property definition.
first create the instance of the appdelegate like and then you can access, whatever variables you have declared in appdelegate as property,synthesize.There is no need to create seperate variable of indNavControl.

Reload data on UITableView

how to reload in UITableView class.
I am using the reloadData method but it's not working.
- (id)initWithCoder:(NSCoder *)coder
{
if (self = [super initWithCoder:coder])
{
[self init];
}
return self;
}
-(id)init
{
appdelegate=[[UIApplication sharedApplication]delegate];
[appdelegate readLiteratureFromDatabase];
tbl_obj.backgroundColor=[UIColor clearColor];
tbl_obj.delegate = self;
tbl_obj.dataSource = self;
[tbl_obj reloadData];
array_content = [[NSMutableArray alloc] init];
return self;
}
Here the init method call in another class. So at that time table are not reload.
please help!
From looking at your code it appears that you really need to read this;
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html
You should not have a method called init that does not call up to super, that will eventually hose things.
Setting your docs ivar to an empty immutable string does nothing but waste memory. Eventually you will need to set docs to something real, wait till that point to do any allocation.
Next download the TableViewSuite sample from here;
http://developer.apple.com/library/ios/#samplecode/TableViewSuite/Introduction/Intro.html%23//apple_ref/doc/uid/DTS40007318
And take a look at the simple sample.
Please take the time to study that sample, it will help you understand table views.
May be that you might not have attached DataSource and Delegate to your XIB
or
You can do it through code using:
self.tableView.delegate = self;
self.tableView.datasource = self;
Also you have to add it as below:
#interface <YourViewControllerName>:UIViewController <UITableViewDelegate,UITableViewDataSource>
EDIT:
But you should try and understand that your controls like UITableView cannot be directly called and set into your UIView class.
It should be UIViewController only then it is possible to add and fillup the data into the TableView.
So your option is to call the data delegate and datasource methods and also the reloadData on the ViewController which works as parentView for the this UIView.
So unless you do this way, I fear that it is not possible.
Hope this helps you.
UIView does not have a reloadData method. UIView Class Reference
Maybe you can show your code so we can see what you are trying to do?
I had the same prob and i just used the viewDidLoad to add the data and in the end i used [super loadView]; and it reload.