iPhone / Objective C - Communication between ViewController / best way to implement? - iphone

I'm doing a iPhone app.
I created my own SwitchViewController Class (linked to the App-Delegate) which actually just
changes the various other Views / ViewControllers (e.g ViewA, ViewB) while the app is running.
When I know recieve a tap-gesture in the ViewController-A I would like to trigger a method (eg. switchViews:(id)sender) in the SwitchViewController Class.
What would the best way to do this?
Must I initiate ViewA with a object-reference to SwitchViewController?
Delegate? What would be the best way?
When I add a button to ViewA its no problem, then I just can connect it to the IBOutlet. But programmatically?
Thanks,
Stefan

Sounds like a prime example for using the NSNotificationCenter.

Used actually the responder chain, and recieved the tap in the switchViewController.

Related

iphone - communicate with previous viewcontroller after segue

Is it possible to communicate with a viewcontroller after a segue switched to another viewcontroller?
I want to know, because I need data from a viewcontroller passed on to the next viewcontroller,
but I just can't figure out how to get data from one viewcontroller to another, as they have no unique names or something which I can use to communicate with.
So can someone tell me:
If it is possible to communicate between viewcontrollers, and if possible, how?
Usually you do it the other way around, pushing values from the source to the destination. If you implement prepareForSegue in the view controller that is going to be segue'd out, you can use
[segue destinationViewController]
to get a reference to the destination view controller.
Then you can set any values in that controller that are needed using properties on that controller before it segues in.
I have searched for tutorials about making a MVC based iPhone app, but without success
Goto ITunesU and look for Paul Hegartys "iPad and iPhone Application DEvelopment" from stanford university
very good!
try the views presentingViewController property.
A typical OOP pattern is to create yet another object, a Model object (MVC paradigm), connect all the view controllers that need to communicate with this Model object, and pass any shared state or variables by setting and getting properties in this Model object. A common shortcut for very small apps is to use the App Delegate as a Model object, as any other controller can get a reference to the app delegate. But this shortcut is not very extensible to larger or reusable code.

Shake event detection in all view controllers

Is this possible in iOS, or do I really have to register a shake event detection in every single view controller?
I want that a user of my app can shake his iPhone to return to the root. Regardless of the current view controller.
I've done this by writing a category on UIViewController. This way you don't need to subclass anything, you can implement it even if the user interface has been finished already.
In my implementation, the VCs will respond to the shake unless the specific VC has opted out of the mechanism. Opting out is done by setting a BOOL ivar to NO in the specific implementation. The shake mechanism will look for this ivar using key-value-coding and ignore the shake if the ivar has been set.
I'd further refine this by allowing only the VC to respond which is currently visible.
edited post (old information was wrong)
in your case I would write my own ViewController which subclasses UIViewController, implements the motion-delegates of UIResponder and will then call the popToRootViewController on the navigationController-property of the ViewController. And everytimes you create a new ViewController you should subclass your ViewCOntroller and not UIViewController. So every ViewController is able to receive the shake-event but it is only written once in your code :)
This is just a guess, but maybe you could do it by subclassing UIApplication (not your app delegate, the actual application). UIApplication is a UIResponder, so you can make it the first responder, and provide a motionBegan or motionEnded method on it.

How to set the delegate to another ViewController?

I've recently started developing for the iPhone and so far I'm doing pretty good but there's this basic pattern I really don't seem to get.
Say, I have a TabBar with two views and a custom delegate protocol, thus my structure is the following:
AppDelegate.h/.m
myDelegateProtocol.h
FirstViewController.h/.m
SecondViewController.h/.m
MainView.xib
FirstView.xib
SecondView.xib
Now I want to achieve the following: I placed a button in the FirstView.xib and I'd like the IBAction which it invokes (inside FirstViewController ofc.) to send a message to the SecondViewController ([self.delegate tellSecondViewContrToSayHi]) and invoke another method which simply prints a log into the console saying "hi I'm here."
So far I know what I need to do in theory:
Specify the protocol.
Implement the protocol in the SecondViewController.
Create an id< myDelegateProtocol > delegate inside my FirstViewController,...AND last but not least:
Set the self.delegate = secondViewControllerObject.
Now, nr.4 is where the problem's at. How on earth do I link the delegate to the other viewController? I mean I'm not the one instantiating the views as the tabBar kinda does that for me,... any advise? Or am I just way too tired to notice a really stupid thing I did somewhere?
Theoretically the same question also applies to the target:action: thing,... I mean, how do I define the target?
Thanks a lot,
wasabi
You have the right idea, assuming that you want relatively tight coupling between these controllers via that delegate protocol.
Since neither controller knows about the other until that delegate property is set you need to have some object which has a reference to both of them wire up that relationship. In your case that's probably the application delegate which can create both controllers, set one as the delegate of the other, and pass both along to your tab bar controller.
What you might actually want is to have the app delegate give both controllers a reference to some shared model object. Your FirstViewController can update that model when you tap a button and your SecondViewController can observe changes to the model to update it's display (or just update its view when it appears based on the current model state). That way your controllers don't need to know anything about each other.

What is the best method to use data in different view controllers?

I am new to ios development and I am trying to use some methods in different view controllers in regular intervals of time (using same methods in different view controllers).
Can anyone please tell me what is the best method for this?
Thanks in advance!
I'd suggest using protocol for this. You can never know when you want different method behaviour for different controllers. And you can always implement default behaviour in private category of generic uiviewcontroller.
Maybe you could try to implement this method in a customViewController that would be subclassed by these viewController and than every viewController would have access to these method.
Alternate way would be to implement it on the appDelegate or on a common tabBarController, navigationController (or whatever) and so every viewController would have access to it.
If the time interval has to be the same for every viewControler, you should do only one time interval and iterate on your viewControllers

custom Copy Responder in iphone

I have a webview in my application. I want to handle when user copies something from web view, my custom method - should be triggered. I have tried following.
I have placed following method in myViewCtr.m file
-(void)copy:(id)sender{
NSLog(#"hi ! Hello ");
}
But nothing working - what should I do to implement the same ?
You need to implement -copy: on the responder, not on the controller. So you need to subclass UIWebView and override -copy:.
Ahh ! I got something here from this question.
Direct sample from Apple itself.
Theory :)