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
Related
I have 2 view controller:
Index
Second
In Index I have 2 textfield and button.
I fill this text field and press "next button" to second view, I need to "show" this two text field, by using Array (later I will send JSON data).
I Tried this:
Declare an #property in class Second as textArray. And then while pushing to Second from Index, you need to set this property.
In Second.h file,
#property(nonatomic, strong) NSArray *textArray;
In Index.m file,
Second *aSecond = [[Second alloc] init];
aSecond.textArray = #[textField1.text, textField2.text];
//write code to push from Index to Second
Now in class Second, you can use it as aSecond.textArray[0] and aSecond.textArray[1]
But if i switch page (click next) array has been nulled.
For switch page i use this:
SecondPage *SecondPage = [self.storyboard instantiateInitialViewController];
SecondPage = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondPage"];
[self SecondPage animated:YES];
How I can best realize? Please help! Best regards
Ivan.
Write your own designated initializer for second view controller something like.
-(id)initWithTextArray:(NSArray *)_textarray;
Now alloc init the second view controller
Second *aSecond = [[Second alloc] initWithTextArray:array];
in the viewDidLoad method of second view controller use your textarray to populate your views.
You need to do it like this if you are using it via storyboard,
SecondPage *aSecondPage = [self.storyboard instantiateInitialViewController];
aSecondPage = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondPage"];
aSecond.textArray = #[textField1.text, textField2.text];
[self aSecondPage animated:YES];
When you do this aSecond = [[Second alloc] init]; and then do aSecondPage = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondPage"];, it is creating a separate instance and not the same instances. You were setting this array on the first one and expecting it to be set on the second instance.
Hey had you tried for 'Global variable & Array with Delegate Method'. It'll not release till you'll remove. It'll be global till the application instance.
In Delegate Class,
.h File
NSMutableArray *obj;
.m file,
Synthesize & Alloc - init array.
Then,
In ViewContoller .m file
AppDelegate *app;
app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
then,
[app.obj addObject: text1.text];
In Second .m file, you can use it.
NSLog(#" Data : %#", app.obj);
Keep in mind, whenever you want this array, you should initialize 'app' object of 'Delegate' class.
It'll work & stores data globally.
Thanks.
I can think of two methods:
1) Either add a method in Second and after initializing call it and pass the value as an argument to a method. so in Second.m -(void) push :(NSArray *)array{} pass it with "&" before your NSArray name.
2) Create a getter in First. Since its NS, you have to use assign so in First.h you put #property(nonatomic, assign) NSArray *array1;
In First.m you add the #synthesize array1;
and call it with
self.array1 = [#"t", #"e", #"s", #"t"];
And then pass the reference to First class in Second and create a variable in Second.m and get it
NSArray *array1 = first.array1;
You can write your own getter, but not reasonable.
SecondPage *SecondPage = [self.storyboard instantiateInitialViewController];
SecondPage = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondPage"];
[self SecondPage animated:YES];
1) Your variable name and your class name are the same
2) I'm not sure XCode accepted that you wrote [self SecondPage animated:YES];
Finally, did you alloc your array ?
EDIT;
Oh and :
aSecond.textArray = #[textField1.text, textField2.text];
You should write:
[aSecond.textArray addObject:textField1.text]; // using NSMutableArray
There are lot of ways to transfer the value from one controller to an other, but for you I think this will be the most simple ever. Use NSUserDefaults
First Easy Ways
in first Controller
[[NSUserDefaults standardUserDefaults] setObject:#[textField1.text, textField2.text] forKey:#"listOfData"];
in 2nd Controller
NSArray *array = [[NSUserDefaults standardUserDefaults]objectForKey:#"listOfData"];
2nd Easy ways is
DetailVC *detailVC = [self.storyboard instantiateInitialViewController];
detailVC = [self.storyboard instantiateViewControllerWithIdentifier:#"DetailVC"];
detailVC.textArray = #[textField1.text, textField2.text];
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.
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.
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.
I have an iPhone app with a tableviewcontroller. When you click a certain cell it opens a new uiviewcontroller with this code:
nextViewController = [[avTouchViewController alloc] initWithNibName:#"avTouchViewController" bundle:nil];
The uiviewcontroller above called avTouchViewController has a property that looks like:
IBOutlet SomeObject *controller;
SomeObject is an object with all relevant view properties.
I would like to pass an nsstring parameter from the tableviewcontroller I initialize the avTouchViewController with to someObject.
How can I do this?
I'm a little confused by your question; you say you're creating your avTouchViewControllers when a cell is tapped inside an existing UITableView, but your last part describes the inverse situation.
Basically, if you want to pass information to a view controller, just give it a property that can be set (which may already be the case), e.g.:
nextViewController = [[avTouchViewController alloc] initWithNibName:#"avTouchViewController" bundle:nil];
nextViewController.controller = theInstanceOfSomeObjectIWantToPass;
You also may want to rename your controller property. To a reader, it doesn't make sense that a view controller has a property called controller which is actually a SomeObject*. As well, your class names should be capitalized, i.e. use AvTouchViewController instead of avTouchViewController.
If I were doing this I would add my own initializer to my UIViewController subclass:
- (id)initWithController:(NSString *pController) {
if (self = [super initWithNibName:#"avTouchViewController" bundle:nil]) {
self.controller = pController;
}
return self;
}
And then just call the following (in tableView:didSelectRowAtIndexPath: or whereever):
NSString *controller = #"Sample String";
AVTouchViewController *nextViewController = [[AVTouchViewController alloc] initWithController:controller];
[controller release];
[self.navigationController pushModalViewController:nextViewController animated:YES];
[nextViewController release];
As a point of style, class names conventionally begin with uppercase letters (hence my change to AVTouchViewController).