2011-11-24 13 views
10

मैं एक टैप के साथ कहीं भी UIAlertView को खारिज करना चाहता हूं। मैं बिना किसी बटन के UIAlertView दिखाना चाहता हूं।आईओएस कैसे एक टैप के साथ UIAlertView को खारिज करने के लिए?

मेरे पास मानक UIAlertView कोड हैं, लेकिन मुझे इनपुट की आवश्यकता है, अगर यह संभव हो तो इसे कैसे खारिज कर दें।

यूआईटीच के साथ? UITapGestureRecognizer के साथ?

धन्यवाद।


संपादित करें:

viewDidLoad में

alertview आरंभीकरण नाम "अलर्ट"

 if (alert) 
    { 
    emptyview = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)]; 
    emptyview.backgroundColor = [UIColor clearColor]; 
    [self.view addSubview:emptyview]; 
     [emptyview addSubview:alert]; 
    NSLog (@"emptyview is there!"); 

     UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; 
     [emptyview addGestureRecognizer:singleTap]; 
     [singleTap release];    
    } 

लेकिन इस emptyview नहीं करता है सब पर जवाब नहीं है और यह handleSingleTap चयनकर्ता का जवाब नहीं है के साथ यहाँ , जिसे मैं थोड़ा सा लिखता हूं:

-(void)handleSingleTap:(UITapGestureRecognizer *)sender{ 
    [alert dismissWithClickedButtonIndex:0 animated:YES]; 
    [emptyview removeFromSuperview]; 
} 

चेतावनी दिखाए जाने पर मुझे इस खाली दृश्य को अलर्ट पर होने की आवश्यकता है, तो मैं एक टैप के साथ अलर्ट खारिज कर सकता हूं।

मैंने कोशिश की:

 if (alert) 
    { 
    emptyview = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)]; 
    emptyview.backgroundColor = [UIColor clearColor]; 
    [self.view addSubview:emptyview]; 
     [emptyview addSubview:alert]; 
    NSLog (@"emptyview is there!"); 

     UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; 
     [alert addGestureRecognizer:singleTap]; 
     [singleTap release];    
    } 
बेशक

, चेतावनी handleSingleTap समारोह का जवाब था। मैंने खाली दृश्य के साथ क्या गलत किया?


दूसरा संपादित करें:

क्या मैं इस मामले में हासिल करना चाहते हैं Kindle अनुप्रयोग में एक शब्द, समान कार्य चयन करने के बाद स्पष्टीकरण के साथ एक छोटा सा दृश्य दिखाने के लिए, यदि आपके पास है। शायद मुझे UIAlertView के बजाय UIView बनाना चाहिए? लेकिन किंडल ऐप में छोटा दृश्य इसके नीचे छाया के साथ इतना अच्छा है, यह कैसे संभव है?

उत्तर

10

ऐसा लगता है कि आप अनिवार्य रूप से iOS पर एक "टोस्ट" पुन: बनाने की कोशिश कर रहे से इस दृश्य को हटा सकते हैं करने के लिए देते हैं। अच्छी खबर, किसी ने पहले ही ऐसा कर लिया है। See this project.

संपादित करें: iToast का उपयोग नहीं करना चाहते हैं। मुझे आपकी शैली पसंद है, यह कम कोड है। यहां मैं क्या आया हूं। यह स्पष्ट प्रतीत होता है क्योंकि अन्य ने कहा है कि UIAlertView की मोडल प्रकृति को दूर करने का एकमात्र तरीका स्पर्श घटनाओं को संभालने के लिए पर्यवेक्षण जोड़ना है। लेकिन आपको हर बार मैन्युअल रूप से ऐसा करने की ज़रूरत नहीं है, UIAlertView subclassing पर विचार करें। सिर ऊपर के बारे में setFrame: आकार को समायोजित करने के लिए एक अच्छी जगह होने के लिए @wagashi, मेरा उत्तर को स्वीकार करने के लिए धन्यवाद, और धन्यवाद:

संपादित करें: कुछ इस तरह की कोशिश करो।आपका कोड बहुत टोस्ट-जैसी छोटी चेतावनी बनाता है, हालांकि जब मैंने कोशिश की तो मुझे पता चला कि अगर संदेश लंबा था तो दृश्य अलग हो गया। इसलिए मैंने एक बटन के आकार के बारे में अलर्ट के आकार को कम करने और स्क्रीन पर केंद्रित रहने के लिए setFrame: को संशोधित किया है। ताकि कक्षा सही प्रश्न का उत्तर दे सके "आईओएस कैसे एक टैप के साथ UIAlertView को खारिज कर सकता है?"

NoButtonAlertView.h

#import <UIKit/UIKit.h> 
@interface _NoButtonAlertViewCover : UIView 
@property (nonatomic,assign) UIAlertView *delegate; 
@end 
@interface NoButtonAlertView : UIAlertView 
-(id)initWithTitle:(NSString *)title message:(NSString *)message; 
@end 

NoButtonAlertView.m

#import "NoButtonAlertView.h" 
@implementation _NoButtonAlertViewCover 
@synthesize delegate = _delegate; 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [self removeFromSuperview]; 
    [_delegate dismissWithClickedButtonIndex:0 animated:YES]; 
} 
@end 
@implementation NoButtonAlertView 
-(void)show{ 
    [super show]; 
    _NoButtonAlertViewCover *cover = [[_NoButtonAlertViewCover alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
    cover.userInteractionEnabled = YES; 
    cover.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.01]; 
    cover.delegate = self; 
    [self.superview addSubview:cover]; 
} 
-(id)initWithTitle:(NSString *)title message:(NSString *)message{ 
    if ((self = [super initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil])){ 
    } 
    return self; 
} 
- (void)setFrame:(CGRect)rect { 
    // Called multiple times, 4 of those times count, so to reduce height by 40 
    rect.size.height -= 10; 
    self.center = self.superview.center; 
    [super setFrame:rect]; 
} 
@end 
इस सरल UIAlertView उपवर्ग के साथ

और एक कवर के लिए अपने UIView उपवर्ग, आप इसे के रूप में बस आप होगा के रूप में उपयोग कर सकते हैं एक मानक UIAlertView। इसलिए जैसा:

NoButtonAlertView *alert = [[NoButtonAlertView alloc] initWithTitle:@"Hello" message:@"I am the very model of a modern major general; I'm information, vegitable, animal, and mineral."]; 
[alert show]; 

निकलेगा:

Custom AlertView

+1

उम, मेरा लक्ष्य जटिल और लंबे कोड से बने दृश्य को बनाने या उपयोग नहीं करना है। यह iToast वास्तव में बहुत अच्छा है, लेकिन मुझे आश्चर्य है कि कम कोड के साथ ऐसी सुविधा बनाना संभव है? हमारे साथ इस लिंक को साझा करने के लिए धन्यवाद। – wagashi

+0

यह बेहद सहायक था! मैंने .m: - (शून्य) सेटफ्रेम: (सीजीआरईटी) रेक्ट { \t // [सुपर सेटफ्रेम: सीजीआरईटीमेक (0, 0, rect.size.width, 300)]; \t \t [सुपर सेटफ्रेम: CGRectMake (0, 0, 100, 100)]; \t \t // नीचे दिया गया कोड UIAlertView केंद्रित है। \t //self.center = CGPointMake (320/2, 480/2); \t \t // मेरा कस्टम cgpointmake \t self.center = CGPointMake (200, 400); } ' – wagashi

+0

हां, आपने मेरे प्रश्न का उत्तर दिया था। हालांकि मैं अपने "अलर्ट" में ज्यादा जानकारी नहीं डाल रहा हूं, इसलिए सीजीआरईटीएमके 0,0,100,100 मेरे मामले में बिल्कुल सही है। मुझे आश्चर्य है कि UIAlertView के अंदर टेक्स्ट का फ़ॉन्ट आकार बदलना संभव है? – wagashi

3

UIAlertView को अपने स्क्रीन नियंत्रक (या यहां तक ​​कि खिड़की) में पूर्ण स्क्रीन आकार के साथ नए खाली UIView में जोड़ने के बाद। इस wiew UITapGestureRecognizer

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; 
[view addGestureRecognizer:singleTap]; 
[singleTap release]; 

अब handleSingleTap विधि में आप UIAlertView को खारिज करने और खिड़की

-(void)handleSingleTap:(UITapGestureRecognizer *)sender{ 
    [myAlert dismissWithClickedButtonIndex:0 animated:YES]; 
    [view removeFromSuperView]; 
} 
+1

मुझे कोई पाठ या छवि –

+0

हाँ, अच्छा विचार के साथ एक कस्टम UIButton जोड़ना होगा। यह कोड की कई पंक्तियों को लिखने से बचाता है। किसी भी मामले में आपको टैप एक्शन को संभालने की आवश्यकता है। – beryllium

+1

जिज्ञासा से, क्यों 'UIAlertView' का उपयोग करें? जो भी पाठ और पृष्ठभूमि रंग w/अल्फा आपको पसंद है (या फैनसीयर प्राप्त करें) के साथ अन्य सभी विचारों के सामने एक पूर्ण स्क्रीन 'UIButton' रखें। – XJones

संबंधित मुद्दे