I need some help in QuickDialog. I am using this tutorial QuickDialog but i cannot find what i would like to do in my QuickDialog.
First i have a controller A that will transfer to controller B using QuickDialog, values are in controller A. Now, my problem is how can i access the values when I'm already in controller B.
For example: i have declared QEntryElement *amountEntry = [[QEntryElement alloc] initWithTitle:#"Amount" Value:#""]; in controller A and passed this on controller B, how will i access amountEntry in controller B.
I hope i have explained it well. Please help on this.
You can access all values within the QRootElement. One way to do so is setting the key property of every QElement and afterwards fetch all key-value pairs into a NSMutableDicionary like so:
NSMutableDictionary *results = [[NSMutableDictionary alloc] init];
[fooRootElement fetchValueIntoObject:results];
You can use the onSelected completion code to trigger such action with a QButtonElement
QSection *confirmButtonSection = [[QSection alloc] init];
QButtonElement *confirmButton = [[QButtonElement alloc] initWithTitle:#"Accept"];
[fooRootElement addSection:confirmButtonSection];
[confirmButtonSection addElement:confirmButton];
[confirmButton setOnSelected:(^{[self fetchResultsAndCheckThemAndDismissControllerBMethod];})];
the button will then call the method on controller A which will leave you with a filled dictionary full of sweet information.
Related
I want to make a class registry class that automatically makes a UITableViewCell and when the user presses the button it'll go to that class and run the code.
Currently to do something like that, in the view controller I have to do something like this
Function1Class *function = [[Function1Class alloc] init];
function.view.backgroundColor = [self veryLightGrayColor];
[self.navigationController pushViewController: menu animated: YES];
Then in the data source I make a new case in cellForRowAtIndexPath, and in the delegate I make a case in didSelectRowAtIndexPath for a one line method call.
I'd like to change the code so anyone that wants to add a new class to this page can just do it at one place, something like:
Function1 *function;
[self addClass:function];
And in addClass the "function" passed in will be stored in an array
For the cellForRowAtIndexPath and didSelectRowAtIndexPath cases they can easily call the right methods by storing in an array.
The problem is how do I automate the initialization of each class object passed into addClass?
I could get the user to write code like this to add in their class
Function1 *function = [[Function1 alloc] init];
[self addClass:function];
Then the class object is already initialized, but that means during startup EVERY class object is getting initialized, which is a problem if there is a lot of classes. How can I take out a class object from the array, and then initialize it to use it? The class objects are all different classes, so I can't hardcode arrayOfClassObjects[i] = [[Function1 alloc] init];
Thanks
Classes are objects, so you can store the classes in an array:
NSArray *functions = #[ [Function1 class],
[Function2 class] ];
Then, when you want to instantiate one, you make it indirectly:
id instance = [[functions[0] alloc] init];
So your addClass: should be something like:
- (void)addClass:(Class)cls {
[self.classList addObject:cls];
}
I'm can see that there is a lot of questions regarding this already, but none of them seems to have given me an explanation to why I cannot access my array from another class.
Here's where I wan't to access the array
(XYZPaymentViewController.m)
- (void)viewDidLoad
{
[super viewDidLoad];
XYZMateOverviewViewController *test = [[XYZMateOverviewViewController alloc] init];
NSMutableArray *t = test.mates;
NSLog(#"Count of ThatArray: %d", [t count]);
}
Nomatter what - the log writes out 0 !!
In another class (XYZMateOverviewViewController.h) I declare the array
#property (retain) NSMutableArray *mates;
I synthesize the array in the implementation area in XYZMateOverviewViewController.m
#synthesize mates;
I hope you can help me understand what I'm doing wrong :)
If you want to access an array from another ViewController you have to pass the array between the ViewControllers.
For example:
You have two ViewControllers. 'A' and 'B', so u have to do this:
In the 'A' ViewController, is where you have the array that you want to send to another ViewController. Then create an instance of 'B' ViewController and send the array to him:
CODE OF 'A' ViewController:
NSMutableArray *arrayToSend = [[NSMutableArray alloc]init];
BViewController *bViewController = [[BViewController alloc]initWithNibName:#"BViewController" bundle:nil];
BViewController.arrayReceived = arrayToSend;
[self.view addSubView:BViewController.view];
After, in your BViewController you just have to take your arrayReceived and use it.
CODE OF 'B' ViewController:
NSLog(#"%#", [arrayReceived objectAtIndex:0]); // For example.
Hope i did help you, if u have any question tell me.
What are you doing with the mates array in the XYZMateOverviewViewController init method?
If you're not adding any data to it a count of 0 is just right.
With the line
XYZMateOverviewViewController *test = [[XYZMateOverviewViewController alloc] init];
you are creating a completely new instance of XYZMateOverviewViewController—one that has nothing to do with any other part of your application*. What you should be doing instead is getting a reference to the already-set-up instance of the view controller. That instance presumably has its mates property set up with the data you want.
The general topic of sharing data between different parts of your application—and between different view controllers, in particular—should be covered somewhere toward the beginning of any “introduction to iOS programming” book or tutorial.
* Unless you’re doing something funky with singletons or shared state, of course, but I doubt that’s the case here.
Do you alloc/init the mutable array in the init method?
I'm using an NSMutableArray to populate cells in my tableview. The problem is, when I try adding an object to my array, it replaces all the objects with the one that was added so I always only have 1 object in my array. Because of this, my tableview always has one cell that is just overwritten whenever the below method is called. Am I missing something?
_selectedThingArray = [[NSMutableArray alloc] init];
_currentThing = newThing;
//Note: newThing is the object being passed when this method is called.
//It is the new data that will should passed into the cell.
[_selectedThingArray addObject:_currentThing];
NSLog(#"%#", newThing);
NSLog(#"array: %#", _selectedThingArray);
[self.tableView reloadData];
Is this line:
_selectedThingArray = [[NSMutableArray alloc] init];
executed every time you "try adding an object"? If yes, then this is your problem. Instead of adding an object to an existing array, you replace it with a brand new array and add object to this new array.
You need to either create the array at some point before you “try adding an object”, or create the array in the same place you're doing it now, but only when you have not already created one.
Add this if condition before allocation:
if(!_selectedThingArray)
_selectedThingArray = [[NSMutableArray alloc] init];
Instead of this only:
_selectedThingArray = [[NSMutableArray alloc] init];
Everything will be fixed.
I have the need for a 2-dimensional look-up table for my app. I wanted to do this as simply as possible, so I just made a series of arrays, and then will just call the appropriate array and position within that array whenever needed. However, this takes about 500 arrays... here is a code snippet:
NSArray *key_1 = [[NSArray alloc] initWithObjects:#"1",#"0.5038",#"0.8054",#"4.51",nil];
NSArray *key_2 = [[NSArray alloc] initWithObjects:#"1",#"0.4869",#"0.8009",#"4.7",nil];
NSArray *key_3 = [[NSArray alloc] initWithObjects:#"1",#"0.4708",#"0.7967",#"4.9",nil];
NSArray *key_4 = [[NSArray alloc] initWithObjects:#"1",#"0.4554",#"0.7926",#"5.09",nil];
NSArray *key_5 = [[NSArray alloc] initWithObjects:#"1",#"0.4407",#"0.7889",#"5.27",nil];
NSArray *key_6 = [[NSArray alloc] initWithObjects:#"1",#"0.426",#"0.7853",#"5.46",nil];
NSArray *key_7 = [[NSArray alloc] initWithObjects:#"1",#"0.4133",#"0.7819",#"5.65",nil];
However, I have two concerns. First, if these 500 array declarations cause a momentary hang in the app, where should I put this code so that it executes while still on the app's splash screen when first starting up? Would that be in viewWillAppear?
Second (and this is probably more of a newb question), if I place these array declarations in viewWillAppear or some other method in my FirstViewController.m, how can I make them accessible to methods in my SecondViewController.m?
Thanks!
So you have two options
First: You can load these arrays on different thread (not on main thread). You can use GCD to create a new thread and pass a these array as blocks to work on. By doing this you can solve your first problem "momentary hang".
Second: As Mrunal mentioned "Try using plist files for this. Write your data in that in dictionary format (Key-Value). And fetch only required data using specific key."
I like to option number two but if you have dynamic array please use option number one.
I have a UIViewController in my iOS application that displays a table that is derived from an NSMutableArray. The cells in this table each refer to a unique UIViewController that is called when the user makes a selection. What I am trying to do in my "didSelectRowAtIndexPath:" method is to dynamically create the UIViewController via an NSMutableDictionary that contains keys that match the values in the NSMutableArray that the table is built from, as well as values that contain the corresponding Class names for the respective UIViewController that needs to be called. Because the list is rather long, I figure I need to do this using a for loop, but I am a bit confused as to how to do it. My NSMutableDictionary looks like this:
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:#"aViewController" forKey:#"SelectionA"];
[dict setObject:#"bViewController" forKey:#"SelectionB"];
[dict setObject:#"cViewController" forKey:#"SelectionC"];
and my NSMutableArray that is the basis for my TableView looks like this:
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:#"SelectionA", #"SelectionB", #"SelectionC",...,nil];
How would I obtain a reference to the value inside the cell, and then construct a for loop that would dynamically create the correct viewController that corresponds to the selection made by the user from the tableView, and then take the user to that viewController via the navigationController?
Thanks in advance to all who reply.
Its not a good idea to create many ViewControllers, you should create one ViewController,
and pass the value of they tableCell to it. In other words, you change the data modell of the ViewController, by selecting the cell. But you will present the Same ViewController.
Only in the case that your cells coreesponmd to different types (e.g one cell a road mao, another a text value) , you have to call different ViewControllers.
If you realy need different view contollers, then get the type you want to dispaly from the cell data
in didSelectCellRowAtIndexPath
myAppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
UIViewController *viewControllerToDisplay;
switch (selection.type) {
case MapType:
viewControllerToDisplay = appDelegate.mapViewController;
case Picture:
viewControllerToDisplay = appDelegate.pictureViewController;
}
now push viewControllerToDisplay to navigaton Controller.
If you really need a view controller for each cell, there's no need to use a dictionary to look them up. Since selection will be by index path, an array is a better choice.
Create a custom object that has two properties: the name you want to display in the cell and a pointer to the view controller you want to push when it's selected. Load myArray with these objects instead of strings. When you are populating a cell, select the object that matches the row and use its name. When a cell is tapped, select the object that matches the row and push its controller.
(But, as others have said, if you can use the same controller and only change the data, that's the way to go!)
Here is the solution,
If you know the name of class then store all the classes name in array with dictionary having key ClassName and Xib. I prefer plist to store names but you can use other way also.
And at didselect of table or picker place the code like this,
Class classobject = NSClassFromString([[ClassArray objectAtIndex:row]valueForKey:#"ClassName"]);
id object = [[classobject alloc] initWithNibName:[[ClassArray objectAtIndex:row]valueForKey:#"Xib"] bundle:nil];
[self.navigationController pushViewController:object animated:YES];
First line will convert your string to class.
Now as we have a benefit that id can hold any object so creat the object using id. And finally you have a custom class object you can do whatever you want to do with it,here for just a demo I did navigation.