using segmented control - iphone

okay i have a tabbed view, so two view controllers. in one view controller i have a segmented control and to know which segment was selected i have an action connected to it. like this:
-(IBAction)selectAngle:(id)sender{
clickedSegment = [myAngleType selectedSegmentIndex];
}
here clickedSegment is an integer which i have exposed as a property in my header. now i want to use the value of this clickedSegment in my other view controller but whenever i create an instance of that VC(segmented controller one) and try to use clickedSegment, i always get 0 as the value no matter if i selected other segment 1 or 2. its always zero. where am i going wrong?

How is your second view controller going to know about any property in the first view controller? This is impossible, unless you explicitly get a reference to it.
// in second view controller
FirstViewController *firstVC =
(FirstViewController*)[self.tabBarController.viewControllers objectAtIndex:0];
NSLog(#"clicked segment: %d", firstVC.clickedSegment); // assuming int

You are not using the synthesized setter correctly, you should set it like this:
-(IBAction)selectAngle:(id)sender{
self.clickedSegment = [myAngleType selectedSegmentIndex];
}

Related

FPPopover UITableView return value

i don't know if anyone is using this open source library for replacing UIPopovercontroller for an iPhone.
i'm trying to deploy the FPPopover into my project, everything is working like i want, but the problem is that i'm not able to return any value to my ViewController.
i'm trying this in didSelectRowAtIndexPath
myParentViewController *parentController =(myParentViewController*)self.parentViewController;
but the problem is that self.parentViewController is (null)
i have also another problem, how can i dismiss the FPPopoverController from within didSelectRowAtIndexPath.
I dismissed the view by adding a popoverView property to the table view controller that is popping up (in this case ATableViewController), and then assigning the FPPopoverViewController to that property. Like this:
ATableViewController *aTableViewController = [[ATableViewController alloc] init];
FPPopoverController *aPopoverController = [[FPPopoverController alloc] initWithViewController:aTableViewController];
aPopoverController.delegate = aTableViewController;
aTableViewController.popoverView = aPopoverController;
Then in didSelectRowAtIndexPath of aTableViewController you can just call:
[self.popoverView dismissPopoverAnimated:YES];
If you are trying to return values to the "parent"...since the parentViewController property is null here, you can just make your own property for it (let's call it "parentView"). So when setting up the above you would use:
aTableViewController.parentView = self;
Then you can access any of the properties of the parentView and return values from the aTableViewController that popped up. A bit of a workaround, but that's what I did...hope it helps!

I am not able to get text field value in to next tab viewController. I am using TabBarController with two tabs

I have a TabBarController with two tabs; first is InputViewController and second one is TableviewController.
Input view controller has two text fields when I enter the text, after that I want to receive that text filed value in to TableviewController (in the next tab). It does not receive the value.
Try to use this concept.....
Declare two variables in Second tab that is Tableviewcotroller..
For exa..
NSString *strname;
NSString *straddr;
#property and #synthesize both...
Now, in your *InputViewCotroller*tab, create and initialize object of TableViewcotroller, using this object, access these varibles. Like, you want to get values of textfields.
Tableviewcotroller *objTable = ........
objTable.strname = self.textName.text;
objTable.straddr = self.txtAddr.text;
Implement this concept in your project...Hope this helps you...:)
What ever value you have in input view controller like
NSString*text1=textbox1.text;
NSString*text2=textbox2.tex;
In the method where you are moving to next view you should pass the value to the table view controller like this
DetailViewController*tblViewController=[[DetailViewController alloc]init];
NSString*textValue1=text1;
tblViewController.textValue1=textValue;
[self.navigationController pushViewController:tblViewController];
you should alos have textValue NSString in tableView controller also to assign value

Autorotating only some of the tabs inside a UITabBar? (ios 5)

I have a UITabBar with 5 tabs. I only wish to enable autorotation for a UIViewController that gets pushed onto the stack deep inside tab #3. So to be clear: tap UITabBar item 3, and you get tabbar item #3's root UIView, which should not autorotate. Tap and get another UIViewController pushed onto the stack (via a UINavigationController). Tap again, and get another UIViewController pushed onto the stack. Only here should this UIView autorotate.
The other 4 tabs should not rotate at all--not the root view of the tabs, nor any of the child views of the tabs.
Can someone tell me what approach I should use? I read that every single tab needs to respond "YES" to willAutorotateToInterfaceOrientation.
In each view's shouldAutorotate..., you could call a method in the root view controller that checks what is currently being displayed. If the deep-level view for tab 3 is on display, it will return YES, otherwise NO, and the views will, in turn, return the same.
Edit -- more detail per user798719's request:
Your root view controller knows which view is on display. You add a method to the root view controller - (BOOL) isDeepLevelTab3Displayed;. The method checks whether the deep-level view for tab 3 is on display and, if so, returns YES, otherwise returns NO.
Each sub view controller’s shouldAutorotate… method will get a ref to the root controller so that it can call isDeepLevelTab3Displayed.
If you’re using a navigation-style controller, you can get the ref like this (self is the sub controller):
NSArray *arrayOfControllers = [self viewControllers];
UIViewController *rootController = [arrayOfControllers objectAtIndex:0]; // per UIViewController class ref, root controller is at index 0
Or you could get anything in your project like this:
YourProjectAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
UIViewController *rootController = appDelegate.rootController; // or appDelegate.intermediateClass1.intermClass2.rootController — however you set up your project
So every sub controller would do this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
UIViewController *rootController = [[self viewControllers] objectAtIndex:0];
return [rootController isDeepLevelTab3Displayed];
}
Therefore, every subcontroller would return YES when autorotation should happen, fulfilling the requirement you mention at the end of your question.
However, if all your subcontrollers are instances of UINavigationController, you could determine which view is currently on display directly, by calling visibleViewController. Then you just need a way of checking its identity.
You could check the controller’s nibName or title, for example, against a constant, or add an integer property intControllerIdentity to all your controllers and set them in the controller’s initWithNibName…
The integer-property scheme might be best, because it won’t be affected should you later change the nibName or title.
You’d add constants to some class whose h file is imported by all the controllers (or, if all the controllers are instances of the same class, put these constants in that class’s h file):
#define kFooController 1
#define kBarController 2
#define kRotatableController 3
And you’d set it like this:
self.intControllerIdentity = kRotatableController;
And check it like this:
if (self.intControllerIdentity == kRotatableController)
Hope that helps. But evaluate this added detail with a critical eye; I have worked with autorotation but not yet with navigation controllers.

Cant access an int value

i declared an int value as my imageIndexForSend through the following code in myView class.
int imageIndexForSend;
#property int imageIndexForSend;
#synthesize imageIndexForSend;
after on a button click i am displaying a popover which is PopOver calss.
there is table view with multiple indexes in popover class.when i click on any row in PopOver class table it set myView class imageIndexForSend as
In PopOver
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
myView *obj = [[myView alloc] init];
[obj getImageForSend:indexPath.row];
[staticSceneController release];
}
in myViewClass
-(void)getImageForSend:(int)index{
imageIndexForSend = index;
}
then i am return to myViewClass after dismissing popover (popOver class) and doing some actions in myViewClass.
then i am clicking a send button.but the integer value imageIndexForSend is zero.cant get the old value which i set from PopUp.
can any one tell me a way to get the old imageIndexForSend value.may i know what mistake i done.
First, you name a method with get to set the value, it's bad.
Second, you use a property and synthesize it, so you don't need to rewrite the set method unless you need to have a custom set method.
And finally you create a new view on each selection of tableview cell !
1) Remove your getImageForSend: method, you don't need that with property
2) Instead using : [obj getImageForSend:indexPath.row];, use : obj.imageIndexForSend = indexPath.row;
3) Don't create a new view on each selection, assign the value on the existing view.
A better way to transmit data from your popover to your view (controller ?) is to have a delegate property in your popover class and set it with your view object, create a delegate protocol with a method that is called when a cell is selected in popover with an int argument (the index) then make your view class adopts the protocol and do a obj.imageIndexForSend = argument; in your protocol method.
It seems you're allocating a myView instance and assigning that to a local variable (obj), but then you don't keep a pointer to that new instance anywhere.
From what I understand, you already have an existing instance of myView, so what you need to do is to set the variable on that instance, and not create a new one every time.
Each instance have their own set of variables, so changing it in a new instance won't affect any other instances.
You are instantiating a new MyView whenever the user taps on any row of your UITableView. You should try to access the original MyView instead (or whatever object shall retain that setup value).
Within your popover, you should find a way to access the instance that holds the actual index-value. How exactly that is achieved depends a lot on your implementation.
In other words, do not instantiate something within an object that has a shorter lifetime than the object that will access that very instance.
If you're trying to access the index of the selected row in the UITableView you can just use the following:
int index = [myTableView indexPathForSelectedRow].row;

iphone - view init issue

I have come across a strange behavior...
In class A, at the viewDidLoad method I do:
b = [[B alloc] initWithNibName:#"B" bundle:nil]; //init the B class (declared as B* b)
[b setText:#"ABCDDEEE"]; //change the text of a UITextView in b
note that b's view is not shown until a button is pressed. However, when I press it and go to b's view, the text of the UITextView is still the "lorem ipsun" text
Once b's view is shown once, I can change the text.
Anyone know this issue and how to solve it??
That is expected behaviour. initWithNibNamedoes not guarantee complete initialization before viewDidLoad is called.
Create an NSString #property in B. Set that property when you load b--not the .text property of a UITextView, but an NSString data object in the view controller. Then in B's -(void)viewDidLoad, set the UITextView's text property with the string you set.
NIB elements don't necessarily exist when the parent view controller first instantiates a new view controller, but you can talk to data fields and then load that data into the view hierarchy members in -(void)viewDidLoad.