Changing view controller when label is certain text? - iphone

I have a QR code reader that will change the label of a text whenever it scans something. For example, if I scan a QR code for "nfl.com" it will display "nfl.com" on the label. I want to switch controllers whenever a certain text appears on the label. This text would basically be an account number so you scan the QR code, the account number appears, it automatically changes you to a different view controller where an array of items for that account number appears.

If you want to check if the string is equal to a specific string, you could do something like the following when you set the text of the label:
label.text = string;
if ([string isEqualToString:#"Specific String"]) {
ExampleViewController *exampleViewController = [[ExampleViewController alloc] init];
[self.navigationController pushViewController:exampleViewController animated:YES
}
If you want a delay so that it shows the label, then after a delay push the next view controller you could do the following:
label.text = string;
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//code to be executed on the main queue after delay
if ([string isEqualToString:#"Specific String"]) {
ExampleViewController *exampleViewController = [[ExampleViewController alloc] init];
[self.navigationController pushViewController:exampleViewController animated:YES
}
});

I tried
[_lblStatus performSelectorOnMainThread:#selector(setText:) withObject:[metadataObj stringValue] waitUntilDone:NO];
if ([_lblStatus isEqual:#"A123"]) {
NSString * storyboardName = #"Main_iPhone";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"CheckViewController"];
[self presentViewController:vc animated:YES completion:nil];
}
I do get the label to change to A123 but I cannot get a switch to a different view controller to happen after this. I tried your answer #ferris but I cannot set my _lblStatus = string; (string results in an error unless I define it in my .h file.

Related

StackScrollView issue with delegate ID MBProgressHUD

I've got a stack scroll view app (like the Twitter and Facebook apps) using PSStackedView
It's creates the view with this stack:
AppDelegate
// set root controller as stack controller
MenuRootController *menuController = [[MenuRootController alloc] init];
self.stackController = [[PSStackedViewController alloc] initWithRootViewController:menuController];
self.window.rootViewController = self.stackController;
[self.window makeKeyAndVisible];
Root nav controller has a UItable, a cell touch loads the next view
// Load Home Stories table
PSStackedViewController *stackController = XAppDelegate.stackController;
UIViewController*viewController = nil;
while ([stackController.viewControllers count]) {
//NSLog(#"launchStories");
[stackController popViewControllerAnimated:YES];
}
viewController = [[TestView alloc] initWithNibName:#"TestView" bundle:nil];
((TestView *)viewController).indexNumber = [stackController.viewControllers count];
viewController.view.width = roundf((self.view.width - stackController.leftInset)/2);
if (viewController) {
[XAppDelegate.stackController pushViewController:viewController fromViewController:nil animated:YES];
}
In this view I want to use the MBProgressHUD (https://github.com/matej/MBProgressHUD/) to display a nice loading XML message
HUD = [[MBProgressHUD alloc] initWithView:self.view.window];
[self.view.window addSubview:HUD];
HUD.delegate = self ;
HUD.labelText = #"Loading";
[HUD showWhileExecuting:#selector(myTask) onTarget:self withObject:nil animated:YES];
But
HUD.delegate = self ;
Throws an warning and the app crashes
Assigning to 'id<MBProgressHUDDelegate>' from incompatible type 'TestView *'
I've tried all sorts of combinations to try and find the current controller but to no avail, I can find the width of the current controller for instance with
PSStackedViewController *stackController = XAppDelegate.stackController;
NSLog(#"%f",stackController.view.width);
which prints 748.000000. But I can't find work out what 'self' should be.
Any ideas?
TestView needs to implement the MBProgressHUDDelegate protocol. In TestView.h make it look something like this:
#interface TestView : ClassYouInheritFrom <MBProgressHUDDelegate>

how to copy a string from one view and display it in the next view?(iphone)

hi everyone i am new to iphone development.
Actually i am trying with some sample app where i have a textField in the Viewcontroller and a Button,when i enter a string in the textfield and press the button it should display the same string in NextView.so can anyone help me out in doing this.
i worked with transition between one view to another view,but i need copy string from Viewcontroller1 to NextView
#ViewController1
-(IBAction)next
{
NextView *Nview = [[NextView alloc]initWithNibName:#"NextView" bundle:nil];
[self.view addSubview:Nview.view];
}
Set an ivar in your NextView called "newString" for example, then pass a string to that ivar form your first controller.
For Example, not tested (and this is one of many ways you can do this):
FirstView
NextView * next = [[NextView alloc] initWithNewString: myTextField.text];
[self.navigationController presentModalViewController: next animated: YES];
NextView
#synthesize newString
-(id)initWithNewString:(NSString*)someString
{
newString = someString;
return self;
}
Then throughout your NextView, just call upon newString wherever you want to get the value of the previous views textField.
-(IBAction)next
{
NextView *Nview = [[NextView alloc]initWithNibName:#"NextView" bundle:nil];
Nview.string_Object=string_to_copy;//declare string_Object in NextView.h
[self.view addSubview:Nview.view];
}
Try this:
In your first view while naviagting:
NextView *nav = [[NextView alloc] init];
nav.textFieldValue = textField.text;
[self.navigationController pushViewController:nav animated:YES];
And in your NextView's .h file create a property:
#property(nonatomic, retain) NSString *textFieldValue;
And in .m file synthesize it like:
#synthesize textFieldValue;
Now you can use textFieldValue in NextView class
P.S: Don't forget to release it :)
As you are a beginner, this will be a good guidance for you to understand the exact flow and it will make you learn how to pass values from one class to another.
Here is an approach (haven't tested, but should work)
Give your textField a tag value like
textField.tag = 1;
and in NextView access it like
UITextField *parentViewTextField = (UITextField*)[self.superview viewWithTag:1]

data passing in navigation based application

i am working on a navigation based application
in the rootviewcontroller i hav a few UITextFields. There is a button on rootviewController, on clicking that button i am changing the view using pushviewcontroller. I want to use values entered in these UItextFields in mapview ie my 2nd view.
PLZ suggest me something.
you could pass all the information to your second controller before pushing to navigation stack.
See the pseudo code for your reference :
First:
MyMapController* myController = [MyMapController alloc] initWithValues:Value1,value2,value3,......valuen];
[self.navigationController pushViewController:myController animated:YES];
[myController release];
myController = nil;
Second:
MyMapController* myController = [MyMapController alloc] init];
myController.value1 = value1;
myController.value2 = value2;
myController.value3 = value3;
............
myController.value7 = value7;
myController.value8 = value8;
[self.navigationController pushViewController:myController animated:YES];
[myController release];
myController = nil;
There could be more approach for sending the data,
Before pushing assign the values to the second controller.
Say you have to send value of textfield1. Now
secondController.textfield1 = textfield1;
Then do your pushing of controller.
Use an NSDictionary object to store the values from the first controller. Say you are using two text fields – textField1 and textField2.
- (void)loadSecondController {
NSDictionary *values = [NSDictionary dictionaryWithObjectsAndKeys:textField1.text, #"textField1", textField2.text, #"textField2", nil];
// Assuming you are using IB files
SecondViewController *controller = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
controller.values = values; // Declare a NSDictionary property 'values' in SecondViewController
[self.navigationController pushViewController:controller animated:YES];
[controller release];
}
Now in SecondViewController, you can access them as
...
textField1Value = [values objectForKey:#"textField1"];
textField2Value = [values objectForKey:#"textField2"];
...

dismiss UIPopOverController from contentViewController

How can you dismiss a popover from the contentViewController, so in the example code I have below I would like to dismiss the UIPopOver from the ProfileViewController code. How do I do this? Another post similar to this suggested to use NSNotification, but how do use it?
- (void)profilePop:(UITapGestureRecognizer *)recognizer
{
ProfileViewController * profile = [[ProfileViewController alloc] init];
CGPoint location = [recognizer locationInView:self.table];
NSIndexPath* indexPath = [self.table indexPathForRowAtPoint:location];
ConvoreCell* cell = (ConvoreCell *) [self.table cellForRowAtIndexPath:indexPath];
profile.uid = [[[self.posts objectAtIndex:indexPath.row] creator] mid];
UIPopoverController * profilePop = [[UIPopoverController alloc] initWithContentViewController:profile];
[profilePop setPopoverContentSize:CGSizeMake(350, 180)];
[profilePop presentPopoverFromRect:CGRectMake(0,0, cell.avatar.frame.size.width, cell.avatar.frame.size.height) inView:cell permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
I would keep a reference to the popover in your profile class.
profile.popOver = profilePop;
then in the area you'de like to dismiss:
[self.popover dismissPopoverAnimated:YES];
I think keeping a reference is the way to go too, but I don't know if its a good idea to retain it, because when you need to release the popover controller it won't dealloc because there is an extra retain.

popOver table view

In my project , On clicking the button the string present in the button Action method should store in popover table view cell.
I cam able to store a single sting , to the first cell ....
And now my problem is i had Four buttons each button action consists of 4 strings , and now the should at a time to the popover table view ,,,
#import "SecondDetailViewController.h"
-(IBAction)viewButtonPressed:(id)sender
{
[super viewDidUnload];
//create the view controller from nib
self.tablePopoverController = [[[TablePopoverController alloc]
initWithNibName:#"TablePopover"
bundle:[NSBundle mainBundle]] autorelease];
////-------------------------------
myArray = [[NSMutableArray alloc] initinitWithObjects:myString,myString2,myString3,myString4,myString5,myString6,nil];
tablePopoverController.getingOrder = myArray ;
NSLog(#"table popo %#",myArray);
tablePopoverController.contentSizeForViewInPopover = CGSizeMake(250, 250);
//create a popover controller
self.popoverController = [[[UIPopoverController alloc]
initWithContentViewController:tablePopoverController] autorelease];
//present the popover view non-modal with a
//refrence to the button pressed within the current view
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
-(IBAction)orderButtonPressed
{
myString = staterlable1.text;
[myArray addObject:myString];
NSLog(#"myArray%#",myString);
}
-(IBAction)orderButton2Pressed
{
myString2 = staterlable2.text;
NSLog(#"myArray%#",myString2);
[myArray addObject:myString2];
}
-(IBAction)orderButton3Pressed
{
myString3 = staterlable3.text;
[myArray addObject:myString3];
NSLog(#"myArray%#",myString3);
}
-(IBAction)orderButton4Pressed
{
myString4 = staterlable4.text;
[myArray addObject:myString4];
NSLog(#"myArray%#",myString4);
}
-(IBAction)orderButton5Pressed
{
myString5 = staterlable5.text;
[myArray addObject:myString5];
NSLog(#"myArray%#",myString5);
}
-(IBAction)orderButton6Pressed
{
myString6 = staterlable6.text;
[myArray addObject:myString6];
NSLog(#"myArray%#",myString6);
my Problem Is after clicking these buttons the myString1 - to - myString6 NSString objects Should Store into NSMutableArray so That i will display all the strings in the TableViewPopOverController which will popover when clicking the another button in the second detailViewController........
thanks in Advance......
The usual way to do this is to embed TablePopoverController in a UINavigationController. Then, in TablePopoverController when handling tableView:tableView didSelectRowAtIndexPath:indexPath push detailViewController into the UINavigationController.
For example, you could build Controller structure the following way (based on your example):
//create a popover controller
self.popoverController = [[[UIPopoverController alloc] initWithContentViewController:[[[UINavigationController alloc] initWithRootViewController:initWithContentViewController:tablePopoverController] autorelease]] autorelease];
It is similar to your popover-creation code, put inserts a UINavigationController under the popover. Now, in TablePopoverController you should handle row selections the usual way with a UINavigationController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetaiViewController *detail = [[DetaiViewController alloc] init];
/* Configure detail using indexpath here indexPath
...
*/
[self.navigationController pushViewController:detail animated:YES];
}
This will work as expected (by pushing the new view into the UINavigationController) because we set controller structure with a UINavigationController.