HMSegmentedControl how to refresh its title - ios

I'm using HMSegmentedControl in my application. I created a HMSegmentedControl instance in view controller A, then jumped to view controller B to collect data, like this:
[self presentViewController:vc animated:YES completion:nil];
Then I went back to view controller A and changed the titles of HMSegmentedControl according to the values from view controller B. I used setSectionTitles: to do the job. The titles did not refresh until I clicked one of them, but I want it refresh immediately. How to do it?
I've tried self.view setNeedsDisplay but did not work.

setNeedsDisplay should do what you need. You just need to call it on the segmented control, not the view controller's view.
[segmentedControl setNeedsDisplay];

Related

After returning from using dismissViewControllerAnimated, clicking any other button crash app

I Have 2 views. View1 and view 2.
I am opening view 2 from view 1 by using
view2 *store2 = [[view2 alloc] initWithNibName:#"view2" bundle:[NSBundle mainBundle]];
[self presentViewController:store2 animated:YES completion:nil];
Then I have added a button on view2 to to dismiss view2 and return to view1 with the code:
[self dismissViewControllerAnimated:YES completion:nil];
The dismiss works well and get back to view1. But each time when I click on any button or any part on view1 after I return there, app crashes.
Kindly guide to go through this. I used this code in past projects and it always worked.
Sounds like no other object is retaining view1's view controller. The view is presented properly since it doesn't need to be retained by anybody in order to be displayed, however, once you try to interact with it, it need its view controller to be alive and responsive. My guess is that if you have view1's view controller as another object's property, or hold it in any other way as long as its view is displayed, you'll be able to use view1's buttons without crashing the app.

show another view controller but save state of current view controller

I'm trying to create a small game where the user moves over an object and displays another view controller to display information. Upon pressing a button on the recently presented view controller, the view gets dismissed and shows the view controller I started off with. I've done this like this:
ViewController.m
OtherViewController *other = [self.storyboard instantiateViewControllerWithIdentifier:#"other"];
[self presentViewController:other animated:YES completion:nil];
OtherViewController.m
[self dismissViewControllerAnimated:YES completion:nil];
but when i dismiss it, the user starts from the beginning again. is there a way to save where the user is and continue from there?
You're presenting the new view controller as a modal. When you dismiss it, the previous view controller should be uncovered in it's previous state.
If it's not, then you need to look at the logic of your ViewController class.
You want your one time setup code in your viewDidLoad method. If you have code in your viewWillAppear:animated method, or your viewDidAppear:animated method that resets the state of your view controller then that is the problem.

Transfering control from 3rd view controller to 1st view controller and invoke a method

Basically i have an application which has 3 screen. 1st screen is a normal UIViewController and 2nd view controller is showing a table view controller and 3rd view controller is a normal UIViewController.
Now user moves from 1st--->2nd-->3rd(Not by Navigation but by present modal view controller).
For ex.
//Moving from 1st to 2nd view controller by Creating an object of 2nd View controller
//SecondViewController *secondViewController=[[SecondViewController alloc]init......]
//[self presentViewController:secondViewController animated:NO completion:nil];
//Moving from 2nd to 3rd view controller by Creating an object of 3rd View controller
//ThirdViewController *thirdViewController=[[ThirdViewController alloc]init......]
//[self presentViewController:thirdViewController animated:NO completion:nil];
But the problem now i am facing is, if some event happens in 3rd view controller i want to invoke a delegate method defined in 1st view controller.But i am not sure how that can be done?
Use NSNotifacation and pass whatever data you want to pass in notification object.
OR
Use #protocol
you can also use the custom delegate for that
here is an example for that http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-custom-delegates/ or http://www.iossdktutorials.com/ios-sdk-custom-delegates/

Refreshing a UIViewController from a Modal Controller

I have a view controller which displays a carousel control (iCarousel). The view is rendered correctly and the carousel is displayed. Right after that a modal is displayed which allows the user to agree to certain terms. I want that once they agree I refresh the viewcontroller which contains the carousel control. Basically, I want to rotate the carousel to some random index.
- (IBAction)accept:(id)sender
{
NewsViewController *newsViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"NewsStoryboard"];
[newsViewController loadNews];
[newsViewController.view setNeedsDisplay];
[self dismissViewControllerAnimated:YES completion:nil];
}
The above code does call the loadNews and fetches it but the view is never refreshed.
What happens to the carousel should really be up to the view controller that manages it, not the modal view controller. Make the modal controller do its thing and return whatever data it collects to its parent. The parent (in this case, the carousel's controller) can then look at that data and decide what it needs to do next (refresh, for example).
The problem is this line:
NewsViewController *newsViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"NewsStoryboard"];
That is not the old view controller; it is a new, unused copy of that view controller. You need to create a line of communication from the modal view controller back to the existing view controller.
The typical way to do this is through a delegate, which you set when creating the modal view controller. If you look at the Xcode Utility template, you will see that it illustrates this architecture. The original view controller sets itself as the modal view controller's delegate, and the modal view controller is thus able to talk back to the original view controller as it is dismissed.
This is such an important thing to be able to do that I talk about it at length in my book:
http://www.apeth.com/iOSBook/ch19.html#_presented_view_controller

Objective-C: How to load a different view on button tap?

I have an iPad application that has a button on one view. When I press the button I want it to load a second view. The second view I am trying to load is a CollectionView. I am not using, and do not want to use a UINavigationController.
Does anyone know how to load a second view on a button tap? Also, I will want to create a Back button that will go back to the previous view. The previous view could be different each time the button is tapped.
There is a decent amount of material online about this topic, but I can't find anything that will work or anything that is recent.
Here is the code I have now:
-(void)showCollectionView:(id)sender
{
NSLog(#"In ShowCollectionView");
ZHCollectionViewController *cvc = [[ZHCollectionViewController alloc]
initWithNibName:#"ZHCollectionViewController"
bundle:[NSBundle mainBundle]];
[self.view addSubview:cvc.view];
NSLog(#"After all the stuff");
}
When this runs both NSLog's are executed and the message shows up in the console, but nothing happens to the view.
Yo can try to present it modally:
[self presentViewController:cvc animated:YES completion:^{
}];
Before this call you can customize appearance of your 'cvc' by defining transition and presentation styles, for example:
cvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
cvc.modalPresentationStyle = UIModalPresentationFormSheet;
To hide it call in ZHCollectionViewController, inside some button action I believe
[self dismissViewControllerAnimated:NO completion:^{
}];
There are several ways to do this, and the way you're trying isn't one of them. If you just want to add a view, not a view controller, you should have a xib file that is a view, not a view controller. You would have to make the controller whose view you're adding this collection view into, the files owner of this collection view, so you can hook up any outlets to it.
It's not correct to add another view controller's view to your view, unless you're making that controller a child controller. If you want ZHCollectionViewController to be the controller of the collection view, then you should add that controller as a child view controller. You can check out Apple's documentation on custom container controllers to see how that's done.
You didn't really say in your question, how this collection view is to appear. Do you want it to take up the whole screen, or do you want it to be a subview? If you want it to take up the whole screen, then it would be better to just change the window's root view controller to ZHCollectionViewController, or present it modally over the current view.

Resources