作者johnlinvc (阿翔)
看板MacDev
标题Re: [问题] 呼叫新的view问题
时间Tue May 15 17:27:25 2012
※ 引述《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
1F:推 lazyasa:谢谢J大,抱歉可否给我一点delegate的作法参考 05/15 17:43
2F:→ lazyasa:老实说我还不太懂delegate的作法该怎麽写 05/15 17:44
3F:→ lazyasa:美服真的是哭哭... 05/15 17:44
4F:→ johnlinvc:所以你是用他的example?还是用你自己的project 05/15 17:55
5F:→ lazyasa:我把的class加到我的project里了 05/15 17:56
6F:→ lazyasa:然後我的optestViewController一直都是on screen的状态喔 05/15 17:57
7F:→ 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)
8F:推 lazyasa:谢谢j大,我想问如果我是用在CustomCell里面的label呢... 05/16 15:04
那就作一个UITableViewCell的Subclass,把delegate设成cell,
再让cell去呼叫viewcontroller 的linkButtonPressed
※ 编辑: johnlinvc 来自: 220.128.223.100 (05/16 15:46)
9F:推 lazyasa:j大,我试了几天,我还是不太知道该怎麽实作 05/23 17:54
10F:→ lazyasa:对不起可以在给我一点提示吗... ,谢谢 05/23 17:54