我正在尝试设置我的应用程序,以便可以将NSUrl属性添加到textview的指定子字符串中,并且当触摸该NSUrl时,它将推送一个新视图而不是打开一个新URL。
我在AppDelegate.cs类中对OpenURL进行了ovverriding,并使用url发送通知
public override bool OpenUrl (UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
{
NSNotificationCenter.DefaultCenter.PostNotification (NSNotification.FromName ("ArtistDetail", url));
return true;
}
然后在包含文本视图的类中,在构造函数中添加观察者
NSNotificationCenter.DefaultCenter.AddObserver ("ArtistDetail", delegate{artistDetail(null);});
有我的方法
public void artistDetail (NSNotification notification)
{
//push view based on info in notification.object
}
我没有运气。我发现这篇文章不能覆盖视图URL,因为有人以相同的方式在目标C中执行了此操作,因此无法推送视图控制器。一个问题-我在我的OpenURL覆盖中放置了断点和日志记录,但是它们没有被击中。单击/点击NSSUrl时,默认情况下是否调用OpenUrl?如果是这样,为什么我的优先选择没有受到打击?
我已经研究了6个小时,该如何做。当然,在将问题发布到SO上20分钟后,我便找到了答案。
我必须实现UIApplication子类,在此重写该方法,注册它,然后在Main.cs中将其作为UIApplication启动。
UIApplicationMain.cs
[Register ("UIApplicationMain")]
public class UIApplicationMain : UIApplication
{
public UIApplicationMain ()
{
}
public override bool OpenUrl (NSUrl url)
{
NSNotificationCenter.DefaultCenter.PostNotification (NSNotification.FromName ("ArtistDetail", url));
return true;
}
}
Main.cs
public class Application
{
// This is the main entry point of the application.
static void Main (string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main (args, "UIApplicationMain", "AppDelegate");
}
}
显示超链接的View Controller,应进行动态处理以推送所需的View Controller
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
NSNotificationCenter.DefaultCenter.AddObserver ("ArtistDetail", artistDetail);
}
public void artistDetail (NSNotification notification)
{
NSUrl artistName = (NSUrl)notification.Object;
String name = artistName.AbsoluteString;
this.NavigationController.PushViewController (new ArtistDetailViewController (name), true);
}
对于任何试图解决“如何在UITextView中放置按钮”问题的人(就像twitter使用#hastags和@users一样)-就是这样!将文本放在NSAttributedString中,将link属性应用于要用作按钮的文本,然后进行相应处理。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句