看板 MacDev 關於我們 聯絡資訊
※ 引述《lazyasa (asa)》之銘言: : Dear all : 不知道今天問問題還有沒有人回.. XD : 大家都D3去了.. : 小弟我最近想把在label出現的文字連結加上url link, : 於是我用了OHAttributedLabel這個套件 : 套件本身點了連結會直接開啟safari : 但我把它改成開啟TSMiniWebBrowser : if (activeLink && (NSEqualRanges(activeLink.range,linkAtTouchesEnded.range) || closeToStart)) { : BOOL openLink = (self.delegate && [self.delegate respondsToSelector:@selector(attributedLabel:shouldFollowLink:)]) : ? [self.delegate attributedLabel:self shouldFollowLink:activeLink] : YES; : if (openLink) { : NSString *url = [activeLink.URL absoluteString]; : optestViewController *viewcontroller = [[optestViewController alloc] init]; : [viewcontroller linkButtonPressed:url]; : [viewcontroller release]; : } : } : - (void) presentModalWebViewController:(BOOL) animated { : // Create webViewController here. : [self presentModalViewController:webViewController animated:animated]; : self.modalWebViewPresented = YES; : } : 的確是可以正常的NSlog出東西 : 但不知道為什麼不會正確呼叫出webViewController : 有大大可以為我解惑一下嗎? : 謝謝 問題在這個optestViewController 根本就沒有on screen 沒on screen 的 view controller 是沒有辦法 presentModalViewController的 比較好的做法是把開url這件事delegate出去給你在螢幕上的view controller。 ps:他的example 寫得有點鳥,完全沒有用到viewcontroller.也沒用到delegateXD. ps2:暗黑美國帳號不知道要等到何年何月才能登亞服... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.128.223.100
lazyasa:謝謝J大,抱歉可否給我一點delegate的作法參考 05/15 17:43
lazyasa:老實說我還不太懂delegate的作法該怎麼寫 05/15 17:44
lazyasa:美服真的是哭哭... 05/15 17:44
johnlinvc:所以你是用他的example?還是用你自己的project 05/15 17:55
lazyasa:我把的class加到我的project裡了 05/15 17:56
lazyasa:然後我的optestViewController一直都是on screen的狀態喔 05/15 17:57
johnlinvc:所以是已經有optestViewController了又再alloc一次? 05/15 18:00
=====OHAttributedLabel.h @protocol OHAttributedLabelDelegate <NSObject> @optional //add following line -(void)openURL:(NSURL *)url; ======OHAttributedLabel.m BOOL openLink = (self.delegate && [self.delegate respondsToSelector:@selector(attributedLabel:shouldFollowLink:)]) ? [self.delegate attributedLabel:self shouldFollowLink:activeLink] : YES; if (openLink) [[UIApplication sharedApplication] openURL:activeLink.URL]; //change privious line to following if (openLink&&[delegate respondsToSelector:@selector(openURL:)]) [delegate openURL:activeLink.URL] =====optestViewController.m 加入下面的method -(void)openURL:(NSURL*)url{ [self linkButtonPressed:url]; } ===== 我不知道你是用IB還是直接call 出label的 反正把 label.delegate = optestViewController; 用IB的話就連連看吧 應該就可以work了 ※ 編輯: johnlinvc 來自: 220.128.223.100 (05/15 18:18)
lazyasa:謝謝j大,我想問如果我是用在CustomCell裡面的label呢... 05/16 15:04
那就作一個UITableViewCell的Subclass,把delegate設成cell, 再讓cell去呼叫viewcontroller 的linkButtonPressed ※ 編輯: johnlinvc 來自: 220.128.223.100 (05/16 15:46)
lazyasa:j大,我試了幾天,我還是不太知道該怎麼實作 05/23 17:54
lazyasa:對不起可以在給我一點提示嗎... ,謝謝 05/23 17:54