How to update a modalViewController after another modalViewController has exited - objective-c

I have a view that is presented when a user clicks on a button (a UIModalPresentationFormSheet). This view has 4 text fields on them, and one button that when clicked pops another (UIModalPresentationFormSheet) view on top. The only thing that this view has is a picker. When an entry is picked, the user then goes back to the previous view. I want to display this entry on the original view after this, but I'm not sure how to update the previous view since the method "ViewWillAppear" and "ViewDidAppear" are not called. Is there a way to send an event to the original modal view when a modal view on top has exited? This way I can update the text of a label from the original once that picker view is discarded.

you can always use delegation.

Related

Nav bar button loading a view controller with no controls in it

I am trying to create a ver simple app in iOS 7. In this I need move back and forward using UIBarButtonItem. But when I am pressing back button it is loading a view but not showing all UI Controls properly basically the view is empty. in the below as I pressed any of the cell it will move to another view and load respective data.
On the pressing back button it should show earlier image but it is showing following view.
Please tell me where I am going wrong Thanks in advance.
It seems as if your second screen is presented modally. Then you need to make an unwind segue.
Add this code to the viewcontroller that should be returned to:
-(IBAction)backButtonUnwindSegue:(UIStoryboardSegue *)segue
{
}
then from your modal vc, in storyboard. Drag a segue from the "Back"-button to the Green "Exit"-icon and select the backButtonUnwindSegue-action
Check Out this:
Passing Data example

View Controllers, Popup buttons, AppDelegates, Status Menus, and headaches

This is kinda long but I want to give as many details as possible.
I have created a status bar app. One of the things I've done is made it to where there is a "Settings" menu item that if you click on, an NSWindow pops up with 5 different buttons -- these buttons are tied to their own custom NSViewController so that if you click them, the View Controller changes, but the window remains the same, just like any other StatusBar app.
On one of the view controllers is a popup button with several user selectable options. If I add a button to the specific VC (view controller) and link that to an ibaction that pulls the selected index of the popup button, it works just fine (i.e. I select item 2, it logs item 2).
Now here's where things get. . . odd. I've created a submenu that would in theory allow the user to quickly change the selected item of the popup linked to my custom view controller. Think of this submenu as being labeled 1-4 and clicking one of those items would change the index of the popup button, however every time I try this and follow it up with a NSLog to ensure the button has changed, it ALWAYS reads zero, in other words, it NEVER changes.
Here's what I tried.
Just like with every other view controller I've ever used, I create a new object of my view controller (alloc, init), and reference the popup button object that i've created as a property. This compiles just fine, but returns only zero no matter what is selected.
I've tried calling the method that I know works (the button that is on the view controller containing the popup), but again, this returns only zero no matter what is selected.
Any advice would be greatly appreciated.
tl;dr: can't change a popupbutton on a view controller from the app delegate or nsstatusmenu item, it just always returns zero.
EDIT: I have found a work around since I can communicate to my app delegate just fine, I created an int property and set it to zero. From there, whenever my popupbutton on my VC changes, I change the value of the app delegate int property and just ensure that whenever the nib launches, it set's the selected item to the app delegate property. I feel there has to be a more straight forward way to just get the value of the popup and change it from the app delegate.
EDIT 2: Some code as requested. I realized I needed to point the the view controller by using initwithnibname from the app delegate and then load the view. Doing so allows me to pull the first object in the popup button, any subsequent changes are not recognized unless I do so via my test button on the view controller itself. Also, I'm adding values to my popup in my awakefromnib in the view controller.
searches_VC = [[Searches_ViewController alloc] initWithNibName:#"Searches_ViewController" bundle:nil];
[searches_VC loadView];
NSLog(#"Selected Title: %#",[[searches_VC profilePopupOutlet] titleOfSelectedItem]);

How to access Navigation Bar from a View Controller of a subview ?

I have a View Controller where I select some data into an array and then optionally on clicking of a button I show that data in a TableView added as a subview to previous view with a flip animation.
Now I have the facility to delete data from array I have picked from first view and passed to this one.
Now I have a condition that there is a button on the righthand side on the navigationBar on whose controller the previous view was pushed.
Now that button needs to be disabled until there r atleast X number of elements in the array.
so while I am adding data In my previous view, I keep check on the array and as soon it crosses the required count , I enable the button.
Now in the other view which is a TableView which has been brought in with animation,
Whenever I delete data I need to disable the button again when it goes below the number X.
How can I access the navigationBar's button in this subview's controller ?
shall I set the first view Controller as delegate in the next one ?
If your viewController is inside UINavigationViewController, you can set navigationBar buttons with self.navigationItem.leftBarButtonItem and self.navigationItem.rightBarButtonItem.
try using superView proprty or loop through all the pushed ViewController and check if it is previous VC. Then make the button enabled or disabled
My opinion is you can create a uinavigationController object in your subview and assign it from your parent view(Where you have the navigation controller object). You can then access the navigation controller in the subview.

Objective C: How to reload tableview immediately after switching tabs?

I have 2 tabbars in my tabbar controller. I am currently in the second tabbar, after I click on a 'done' button, the tabbar controller needs to switch to the first tab and auto refresh the tableview in it.
I am able to execute on the first part
//Switch to the first tab's view
self.tabBarController.selectedViewController
= [self.tabBarController.viewControllers objectAtIndex:0];
However I need some advise on how to refresh the first view's tableview immediately after the switch is made. Any advise is appreciated.
EDIT: Updated requirement
Note: I do not want the view in the tabbar to reload everytime someone clicks on the tab. The reload should only happen in tab 1's tableview when
1) User is in tab 2
2) User clicks on 'Done button' in tab 2
3) App switches to tab 1 + Reloads the tableview in tab 1
Rationale is that the user will update some information in tab 2 which can only be viewed in tab 1. I want the user to be able to view the updated info immediately after switching to tab 1.
You could just do [tableView reloadData] in the first view's viewDidAppear: method?
EDIT: Editing to add additional data.
General caveat here, Apple tends to frown upon having custom UI Patterns like this. I'm not sure what your app is trying to do here, but you should mostly stick with switching tabs only if the user explicitly clicks on that tab. Have you looked at modal view controllers if you want to present a screen and get some data back, instead of having it in a tab bar?
If not, I guess you could have a solution where the first tab is a delegate of the second tab. When the button is clicked, you could call [delegate doneBtnClicked:] so the control shifts to the first tab. In that function, you could use the setSelectedIndex: to your current tab and call reloadData: on the table view.
You can send NSNotification after
pressing done button on second tab
and handle this notification on first tab controller.
So, create a DONE Button as a UIBarButtonItem, and in it's IBAction, do the following.
For reloading the tableView for the view in the first tab, put that tableView as that class' property , and reference the class like #class class1 , and create an object as a property class1 *object1 for View1 (Tab1) in the class for View2 (Tab2), and access the array1 using object1 like
[class1.object1.array1 reloadData];
And for bringing the view to tab one, use
self.tabBarController.selectedIndex = 0;
Thats pretty much it !
Alternatively you can also use a singleton class for this purpose.

TableView Reusabilty using segmented control

I have a view/Window which has a 2 segmented button on the top and a view below it.
When I click on the first button, the tableview with some data gets loaded on the view below. Now I want to use the the same table when I click on the 2nd button, but I want to add a few more section/rows.
The problem is when I click the first button the tableview gets loaded and the number of rows/sections method gets called. But when I click on the second button, those methods aren't called because they are called just once whenever a table is loaded.
So basically I get the same number of rows. I can do this with two different tableviews but I would like to use just one.
Those methods will be called again if you reload the table data. You can do this using this command:
[self.tableView reloadData];