Effective use of Interface Builder

I am new to iPhone and lens c. I spent hours and hours and hours reading documents and trying to understand how everything works. I have RTFM, or at least in the process.

My main problem is that I want to understand how to indicate where the event is being sent, and the only way I could do this is to specify delegates, but I'm sure there is an easier / faster way in IB.

So an example. Suppose I have 20 different views and view controllers and one MyAppDelegate. I want to be able to create all these different Xib files in IB and add, however, many buttons and text fields, etc., and then indicate that they all produce some kind of event in the MyAppDelegate object. To do this, I added a MyAppDelegate object to each view controller as an IB list. Then I created the IBAction method in MyAppDelegate in Xcode and returned to IB and associated all the events with the MyAppDelegate object in each Xib file.

However, when I tried to run it, it just crashed with a read error.

I assume that every Xib file places a pointer to the MyAppDelegate object, which has nothing to do with the possible MyAppDelegate address that will actually be created at runtime.

So my question is ... how can I do this? !!!

+3
source share
3 answers

If you create an instance of MyAppDelegate in each nib file, then yes, you end up with many different instances of the class when all media is loaded. An application delegate is not identified by class or even protocol, but rather as an object pointed to by the property of the application instance delegate. To find a true application delegate, you need to ask the application object itself for its delegate

, appDelegate. - :

#import "MyAppDelegateClass.h"

@interface ViewControllerBaseClass :UIViewController {
    MyAppDelegateClass *appDelegate;
}
@property(nonatomic, retain)  *appDelegate;

@end

@implementation ViewControllerBaseClass
@synthesize appDelegate;

-(MyAppDelegateClass *) appDelegate{
    self.appDelegate=(MyAppDelegateClass *)[[UIApplication sharedInstance] delegate];
    return appDelegate;
}
@end

, self.appDelegate. , self.appDelegate.attributeName.

, . nib.

+4

, , , , . , . , , ( , ).

( UIViewController) XIB. . , 0 1 XIB ( ). Target/Action IBOutlets IBActions (. ). , XIB ( ).

+1

In general, you must create a view controller for each of the views you create and associate events with these view managers, and not with the application delegate. In fact, as a rule, not a single event is ever associated with an application delegate from any nib file, even in project examples you will notice that view controllers are created and held by the application delegate, but it does not accept events.

0
source

Source: https://habr.com/ru/post/1742711/


All Articles