I have navigation controller. On the second view of this controller I have created a back button which action named "back". In this method I call "popViewControllerAnimated" method and problem is, that after "popViewControllerAnimated", I have some other action which is also called in the same time, but after "popViewControllerAnimated" and they are show to me, but I need to block it.
When I press button:
- (void)back {
[socketIO sendEvent:#"exit" withData:nil]; // Send data to server
[self.navigationController popViewControllerAnimated:NO];
}
And the delegate for parsing the result from server:
- (void)socketIO:(SocketIO *)socket didReceiveEvent:(SocketIOPacket *)packet {
if ([packet.name isEqualToString:#"disconnect"]) {
NSLog(#"Exit"); // This code also works, but it works after "popViewControllerAnimated", but I want to block it.
}
}
Scheme:
User press back
App send to server event "exit"
popViewControllerAnimated
App received the answer from server and show result
But how the item 4 in list works after "popViewControllerAnimated", if I closed that View Controller? How I can fix this behavior?
Can you place the communication logic in a central class (probably a singleton)? That has the responsibility to communicate with the backend. When it receives an answer, it sends a notification. Some class should receive the notification and add a new view controller.
Something like what I suggested in this thread: Dismissing view in UINavigationController and at the same time syncing
--- Edit to respond to comment ---
First of all I think it is important that the name of the class below is more connected to the problem you are trying to solve. I gave it a general name, but I'm not happy with the name I choose.
#interface Communicator : NSObject
- (void) sendExit;
+ (Communication *) defaultCommunicator;
#end
In the .c
- (void)socketIO:(SocketIO *)socket didReceiveEvent:(SocketIOPacket *)packet {
if ([packet.name isEqualToString:#"disconnect"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"CommunicatorExit" object:boek];
}
}
A good place for listening to the notification would be the view controller before the current one.
Related
I’m I downloaded MZFormSheetController library for my app.
I’ve got a problem on my popup. When I am on my TableViewController, I tap on a row to get popup to open up so that I can change the name. The popup opens, I set the name and when I tap on the button to correct the name, I call the button method but i can’t close my popup while reload my list.
- (IBAction)modifierTournoi:(id)sender {
//code to update database
//this method close the popup but don't call method viewWillAppear to reload database
//I don't know what method i can use..?
[self dismissFormSheetControllerAnimated:YES completionHandler:^(MZFormSheetController *formSheetController) {
}];
}
Before that, I used the method popViewControllerAnimated to come back to my list while recharging my list.
- (IBAction)modifierJoueur:(id)sender {
//code to update database
[self.navigationController popViewControllerAnimated:true];
}
Can you help me please ?
Thank you very much.
It looks like there is a specific completion handler for this purpose built into the library you are using:
- (IBAction)modifierTournoi:(id)sender {
//code to update database
//this method close the popup but don't call method viewWillAppear to reload database
//I don't know what method i can use..?
[self dismissFormSheetControllerAnimated:YES completionHandler:^(MZFormSheetController *formSheetController) {
// Reloading your database should work in here.
}];
}
The reason viewWillAppear will not be being called is because rather than placing a viewController modally above your window, I imagine MZFormSheetController will be adding a UIView above all presented UIViews, so viewWillAppear will never be called. But as I said above, you should be able to reload in the completion handler block.
I m actually new to developing iOS application. I currently developing an iPad application where there is two UIViewController (A and B).
A is my parent view controller and B is my UITableView popover that don cover the entire A.
After a row select at B, i manage to dismissed B but it don reflect changes made to A.
How do i reload the parent view or is a something like android called the onResume method.
Or ways to solve this problem.
Please provide me with some pointers, have being stuck for hours. Thanks
It depends on the situation. I would suggest 2 ways:
As someone mentioned before, you can make a delegate mechanism so that controller B can call something like -reloadData on controller A. This is a tight coupling but can solve your problem.
You can post a NSNotification from controller B and then listen to it in controller A. In controller B:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Do your logic here
[[NSNotificationCenter defaultCenter] postNotificationWithName:#"SettingsSavedNotification" object:nil];
// Dismiss B controller
}
And in controller A:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didReceiveSettingsSavedNotification:) name:#"SettingsSavedNotification" object:nil];
// Proceed with controller/view setup
}
- (void)didReceiveSettingsSavedNotification:(NSNotification *)notification
{
// Reload data here
}
Don't forget to call -removeObserver:name:object: on controller A teardown.
Use – popoverDidClose: NSPopover class delegate method for update your data, or you may use cocoa binding.
Two things:
1) You want to make sure you are the delegate to the UIPopoverController you are using to show your popover view controller "B". See the docs here: https://developer.apple.com/library/ios/documentation/uikit/reference/UIPopoverControllerDelegate_protocol/Reference/Reference.html
Then you'll want to implement one of those methods, e.g.:
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
// Reload my view controller "A"
}
2) How do you know which row was selected in view controller B? It's possible you're updating some singleton that both view controllers have access to, but a better design pattern might be to create your own protocol and for view controller "A" to conform to it. In that case view controller B should have a weak delegate property that it sends a message to when the user selects a row. Just look at another class that uses the delegate/protocol pattern to see how it works, you could even look at the .h file of UIPopoverController by CMD + clicking the class name, or CMD + Shift + O to the file name.
Cant you just use - (void)viewWillAppear:(BOOL)animated ?
I need to update an image on the main view controller from a pop up view controller.
The button is called 'Feature2btn' on the Main View (EraViewController) but when I try the following code on the popup view controller it won't work.
It needs to be an immediate update as the main view is still showing in the background and does not reload so the change needs to be directly caused by the action on the pop up view.
- (IBAction)purchase:(id)sender {
HomeController = [[EraViewController alloc] initWithNibName:#"EraViewController" bundle:nil];
UIImage *image = [UIImage imageNamed:#"ico_plan.png"];
[(EraViewController*)HomeController setFeature2Btn:[feature2Btn setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
}
There is (at least) two ways to do this:
You use a notification that one controller listens to and the other sends at the appropriate time.
You create a delegate protocol that the first controller implements and the second on calls.
The delegate one is a bit more complicated but generally considered good style. The notification one is not bad, either, but slightly less "elegant".
I will describe the notification based one here, because it seems ok for your case and would also allow to react to the purchase in multiple places by just registering for the notification there, too.
In the controller that has the image to be updated, register for a notification in viewDidAppear::
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateImage:) name:#"UpdateImageNotification" object:nil];
Implement the updateImage: method:
-(void)updateImage:(NSNotification*)note
{
NSString* newImageName = note.userInfo[#"imageFileKey"];
// ... update UI with the new image
}
Also make sure to deregister for that notification when the view goes away:
-(void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super viewWillDisappear:animated];
}
In the other controller, that triggers the update, fire the notification at the appropriate place:
-(IBAction)purchase:(id)sender
{
// ...
NSDictionary* userInfo = #{#"imageFileKey" : newImageName};
[[NSNotificationCenter defaultCenter]
postNotificationName:#"UpdateImageNotification"
object:self userInfo:userInfo];
// ...
}
The object parameter in the notification context is to be used to specify if you want to listen to the notifications by any object or just by a very specific instance. In many cases the actual instance is not relevant, but you just discern the notifications by their name (like "UpdateImageNotification" in this case).
The userInfo dictionary is intended to carry along any information you need to provide with the notification. That's why I introduced a key "imageFileKey" that is associated with the new image name.
I think you made a small mistake. But before going ahead I just want to confirm whether following is the scenario. Correct me if I am wrong
1. You have mainViewController/HomeViewController (of type EraViewController), where you want to update the image
2. On mainViewController you have a popup screen, in which above code is written. The code is intended to change the button image on mainViewController/HomeViewController
If above is the scenario then I suggest following solution.
ERROR YOU MADE
You are creating a new object of EraViewController in the code you posted above and changing the image. According to OOPS concepts, new instance of that controller will be created(a second instance which is not visible on the screen) and you are applying new image in that instance. Now as that instance is not at all visible, you get a feeling that screen is not updating.
SOLUTION
There are at-least 3 solution to this problem
1. One solution will be Daniel Schneller gave in answer (with little bit modifications probably)
2. To achieve this through the delegates.
- You have to write the protocol in the PopViewController and have to implement that in the HomeViewController/mainViewController.
- As the image changes, in PopViewController, That has to be notified to the mainViewController, using delegate and protocol method.
- So that the mainViewController will get the notification and a protocol method will be executed. In that method you should have a code to update the image on the button.
3. (This is not suggested as this is not a good design)You have maintain the instance of the actual viewController which is visible on the screen (in a variable, lets say homeViewController). Then you can use following code in popupViewController
- (IBAction)purchase:(id)sender {
UIImage *image = [UIImage imageNamed:#"ico_plan.png"];
[(EraViewController*)homeViewController setFeature2Btn:[feature2Btn setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
}
Hoep this helps.
Thanks
Try this,
- (IBAction)purchase:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"updateimage" object:imageName];
}
in mainviewcontroller
-(void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateiimage:) name:#"updateimage" object:nil];
}
- (void) updateiimage:(NSNotification *)notification
{
NSString * text =[notification object];
//update Image
}
My goal is to notify a UITableView to refresh itself every time some configurations have changed. The problem is that the configuration view is "not" on the same view that produces the signal. (Yes, I used Tabbed Application.)
Currently I use a sort of global variable in AppDelegate for detecting the change in one view, and do the check in another view. This is fine but the code is not readable as it is so tightly coupling. Is there an elegant method for doing this? Do I miss something in this programming framework?
If there were such an elegant way, I suppose the refreshing process of UITableView should happen as soon as the notification occurs. In this case, I would like to know whether it's possible to delay UITableView from refreshing itself until viewDidAppear occurs.
I would use KVO (Key Value Observing) to keep track of when it changes:
- (void)viewDidLoad {
[super viewDidLoad];
// Note that you can use the options to get the new value passed when it
// changes if you want to update immediately.
[configurationObject addObserver:self forKeyPath:#"configurationItem" options:0 context:nil];
}
- (void)viewDidUnload {
[super viewDidUnload];
[configurationObject removeObserver:self forKeyPath:#"configurationItem"];
}
// Note that I would refresh in viewWillAppear instead of viewDidAppear
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (self.needToRefreshData == YES) {
[self.tableView refreshData];
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (keyPath isEqualToString:#"configurationItem") {
[self.needToRefreshData = YES];
}
}
Use Delegation Design Pattern to pass data from one View Controller to the Other.
For example, let's say one Tab shows a list of cars in a UITableViewController and you have another view that let's a user add a new car to the list. You can let the UITableViewController
Adopt AddCarViewController's protocol
Set itself as a Delegate for AddCarViewController's protocol
Implement its protocol method
Execute the protocol method when informed
You can then let the AddCarViewController
Create a Protocol
Declare object reference Delegate with getter and setter methods
Define a method under that protocol
Inform the Delegate when the Save action is performed
Take a look at the following sample code for your UITableViewController
#interface ViewController : UITableViewController <AddCarViewControllerDelegate>
:
:
// The addCar: method is invoked when the user taps the Add button created at run time.
- (void)addCar:(id)sender
{
// Perform the segue named ShowAddCar
[self performSegueWithIdentifier:#"ShowAddCar" sender:self];
}
:
:
// This method is called by the system whenever you invoke the method performSegueWithIdentifier:sender:
// You never call this method. It is invoked by the system.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSString *segueIdentifier = [segue identifier];
if ([segueIdentifier isEqualToString:#"ShowAddCar"]) {
// Obtain the object reference of the destination view controller
AddCarViewController *addCarViewController = [segue destinationViewController];
// Under the Delegation Design Pattern, set the addCarViewController's delegate to be self
addCarViewController.delegate = self;
// Instantiate a Save button to invoke the save: method when tapped
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:addCarViewController action:#selector(save:)];
// Set up the Save custom button on the right of the navigation bar
addCarViewController.navigationItem.rightBarButtonItem = saveButton;
}
}
:
:
- (void)addCarViewController:(AddCarViewController *)controller didFinishWithSave: (BOOL)save {
:
:
}
Sample code for the AddCarViewController is here
#protocol AddCarViewControllerDelegate;
#interface AddCarViewController : UIViewController
#property (nonatomic, strong) IBOutlet UITextField *carMake;
#property (nonatomic, strong) IBOutlet UITextField *CarName;
#property (nonatomic, assign) id <AddCarViewControllerDelegate> delegate;
// The keyboardDone: method is invoked when the user taps Done on the keyboard
- (IBAction)keyboardDone:(id)sender;
// The save: method is invoked when the user taps the Save button created at run time.
- (void)save:(id)sender;
#end
/*
The Protocol must be specified after the Interface specification is ended.
Guidelines:
- Create a protocol name as ClassNameDelegate as we did above.
- Create a protocol method name starting with the name of the class defining the protocol.
- Make the first method parameter to be the object reference of the caller as we did below.
*/
#protocol AddCarViewControllerDelegate
- (void)addCarViewController:(AddCarViewController *)controller didFinishWithSave:(BOOL)save;
#end
Well, one approach would be to have some common class (singleton perhaps which app delegate kind of is) that keeps track of your model, when the settings viewController detects a change it can mark the model as changed, then when the view in question comes in to view, ie, viewDidAppear gets called, it can query the model to see if the changed flag has been set, if it has then you know to reload the table view, otherwise you dont...
Another way could be to use notification center for it, if your view is loaded it can sign up for the notifications of the model change, in which at point it sets a flag that it needs to reload the table view next time it comes on screen..
hope this helps
You could store the configuration in core data and use an NSFetchedResultsController with the dependant view controller set as a delegate. This way your view controller will get a callback whenever the data is changed.
Apple has some boilerplate code to handle the updates as well
I'm developing an app that needs to pick up a JPEG from a web site on start-up.
The splash screen displays, then the app attempts to get a web address from a file. If the file is missing I open a modal view (as UIModalPresentationFormSheet) that has a text view for the user to type in an address - the address is then saved to a file.
The user taps the OK button, and an attempt is made to get the JPEG. If the address was wrong, or the JPEG is not on the web server, the modal dialog must re-open so the user can change the web address to the correct one.
The splash screen view controller contains these methods:
- (void)openAddressDialog
{
serverView *viewController = [[serverView alloc]init];
[viewController setServerAddress:[businessLogic serverAddress]];
[viewController setDelegate:self];
[viewController setModalPresentationStyle:UIModalPresentationFormSheet];
[self presentModalViewController:viewController animated:YES];
}
Interestingly, when I called the openAddressDialog method from the viewDidLoad method the modal view did not appear. I had to move it to the viewDidAppear method. So presumably the view has to be in a particular state before it will entertain modal views.
- (void)closeDialog:(UIViewController *)dialogController:(Boolean)actionRequired
{
// If action required, get the server address from the dialog
if (actionRequired)
{
serverView *viewController = (serverView *)dialogController;
NSString *address = [[viewController serverAddress]copy];
[businessLogic setServerAddress:address];
[self dismissModalViewControllerAnimated:YES];
if (![logoImage image])
{
[logoImage setImage:[businessLogic eventLogo]];
if (![logoImage image])
{
[self openAddressDialog];
}
}
}
else
{
exit(0);
}
}
This is the delegate method called back from the modal view when the user has touched OK or Cancel. The actionRequired param indicates that OK was tapped. And if so, the new server address is picked up from the modal view, and the modal view is dismissed. An attempt is made to get the JPEG from the new address (in a business rules class), and if still no file can be found, the first method shown above (openAddressDialog) is called again so the user can correct the address again.
The modal view appears fine the first time, but will not reappear if the user entered the wrong address. Does this have something to do with me attempting to represent the modal view so quickly after dismissing it?
I'm quite new to iPad development, so would appreciate any advice.
One other thing, which demonstrates my inexperience of C++ perhaps, is ... if I declare a private method in the m file, let's call it
- (void) methodB
and that method calls another private method, let's call it
- (void) methodA
methodA must be defined earlier in the m file than methodB. If I also want methodA to call methodB I reach an impasse. The only way around that I am aware of is to declare methodB in the h file - which makes it public. How do I code this scenario so the outside world can see neither of the methods?
if use to create nib to serverView then do like this
serverView *viewController = [[serverView alloc]initWithNibName:#"serverView" bundle:nil];