2010-03-05 6 views
5

के लिए एक अलग दृश्य नियंत्रक से फ़ंक्शन को कॉल करना मुझे एक समस्या है जहां मैं किसी अन्य नियंत्रक से एक व्यू कंट्रोलर में परिभाषित फ़ंक्शन को कॉल करना चाहता हूं। मैं कोशिश करता हूं कि एक सौ अलग-अलग सेटअप लॉक लगता है और कुछ भी काम नहीं करता है।आईफोन

मैंने मूल कोड पोस्ट किया है और उम्मीद कर रहा था कि कोई मुझे बता सके कि वे यह कैसे करेंगे। असल में मैं बस इतना करना चाहता हूं कि स्विचब्यू कंट्रोलर में गेम व्यू कंट्रोलर से परिभाषित MYBPress फ़ंक्शन को कॉल करें जब डीलबी बटन दबाया जाता है। किसी भी तरह की सहायता का स्वागत किया जाएगा। पुनश्च: मैं एक लंबे समय के लिए कोडित है, लेकिन Obj सी

// ------- SwitchViewController.h --------------- 
#import <UIKit/UIKit.h> 
@class GameViewController; 
@class OptionsViewController; 

@interface SwitchViewController : UIViewController { 
OptionsViewController *optionsViewController; 
} 

@property (retain, nonatomic) OptionsViewController *optionsViewController; 
@property (retain, nonatomic) GameViewController *gameViewController; 
-(IBAction)MyBPress:(id)sender; 
@end 


// -------- GameViewController.h ------------ 

#import <UIKit/UIKit.h> 

@interface GameViewController : UIViewController { 
    IBOutlet UIButton *dealB; 
} 
@property(nonatomic,retain) IBOutlet UIButton *dealB; 
- (IBAction)dealB:(id)sender; 
@end 


// ------- GameViewController.m 
#import "GameViewController.h" 

@implementation GameViewController 
@synthesize dealB;   // The Deal button 

- (IBAction)dealB:(id)sender 
{ 
    // Here is where I want to call the MyBPress function 
} 

@end 

उत्तर

5

को realtivly नया हूँ "हाय सभी, मैं एक समस्या है जहाँ मैं एक समारोह के अन्य नियंत्रक से एक दृश्य नियंत्रक में परिभाषित कॉल करना चाहते हैं। मैं कोशिश करता हूं कि एक सौ अलग सेटअप लॉक लगता है और कुछ भी काम नहीं करता है। "

ऐसा इसलिए है क्योंकि यह खराब डिज़ाइन है। नियंत्रकों को सीधे एक-दूसरे से बात नहीं करनी चाहिए।

आपको प्रतिनिधियों, अधिसूचनाओं या कुछ साझा केंद्रीय इकाई का उपयोग करने पर विचार करना चाहिए।

+0

आपके इनपुट के लिए धन्यवाद। क्या यह एक खराब डिजाइन है? शायद, लेकिन सवाल यह है कि, यह किया जा सकता है। –

+0

अरे, St3fan, मैं वापस चला गया और NsNotificationCenter का उपयोग कर फिर कोशिश की और इसे काम करने के लिए मिला। मैंने पहले कोशिश की थी लेकिन जाहिर है कि यह सही नहीं कर रहा था। मेरी निराशा ने मुझे अपरंपरागत तरीकों का प्रयास करने का नेतृत्व किया :-) आपकी टिप्पणी ने मुझे फिर से देखने के लिए मिला और मैंने इसे समझ लिया, इसलिए इनपुट के लिए धन्यवाद। –

1

क्यों न केवल एक बटन दोनों संदेशों को सीधे अपने संबंधित नियंत्रकों को भेजता है? UIButton के उदाहरण कई लक्ष्यों को एकाधिक संदेश भेजने में स्वाभाविक रूप से सक्षम हैं। बटन कॉन्फ़िगर करने के लिए आपको निम्न संदेश के रूप में कई बार भेज सकते हैं के रूप में आवश्यक:

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents 

तुम भी इंटरफ़ेस बिल्डर में बटन एक ही बात करने के लिए तार कर सकते हैं। या मिश्रण और अपने दिल की सामग्री से मेल खाते हैं।

+0

उस जानकारी के लिए धन्यवाद। कोशिश करने का कभी सोचा नहीं, लेकिन मुझे भविष्य के संदर्भ के लिए याद होगा। –

25
/* Ok, so based on a suggestion to use Notifications, I solved my problem. It's 
actually so simple, it ridiculous I had so much trouble with it. Thought I'd 
post it just in case some other newbie hits the same type issue. 
*/ 

// in the SwitchViewController.m I added this. This sets it up so 
// when the dealNotification 
// is triggered, the HideBar function I have defined in SwitchViewController 
// gets called. 
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(HideBar) name:@"dealNotification" object: nil]; 


// in the GameViewController.m where I want to call the function in the other controller, 
// I added this and it send the notification to run the function 
[[NSNotificationCenter defaultCenter] postNotificationName:@"dealNotification" object: nil]; 
+0

यह मेरे लिए पूरी तरह से काम किया। – sridvijay

+0

@ रिक यह महान है ... धन्यवाद – death7eater