添加“关闭”按钮到 Three20 TTWebController - iPhone

发布于 2024-10-10 12:40:20 字数 1104 浏览 7 评论 0原文

我使用以下代码将 TTWebController 呈现为模态视图:

TTNavigator* navigator = [TTNavigator navigator];
TTURLMap* map = navigator.URLMap;
[map from:@"*" toModalViewController:[TTWebController class]];

一切都很好,除了 TTWebController 上没有“关闭”按钮。我知道这通常是从侧面推动并给出一个“后退”按钮,但是有没有办法在不深入研究实际 Three20 代码的情况下添加“关闭”按钮?

更新:

这是我用来创建文本然后将模态视图推送到屏幕上的代码。我想动态发送单击的 URL,以代替“YourUrlHere”。

CGRect frame = CGRectMake(100, 100, 200, 200);
TTStyledTextLabel* label = [[[TTStyledTextLabel alloc] initWithFrame:frame] autorelease];
NSString* labelText = @"http://www.yahoo.com http://www.google.com http://www.test.com";
label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES];
[self.view addSubview:label];   

TTNavigator* navigator = [TTNavigator navigator];
TTURLMap* map = navigator.URLMap;
myWebView *newViewController = [[myWebView alloc] initWithNibName:@"TTStyledTextLabelWebView" bundle:nil incomingURL:[NSString stringWithString:@"http://www.YourUrlHere.com"]];
[map from:@"*" toModalViewController:[newViewController class]];

I am presenting a TTWebController as a Modal View using this code:

TTNavigator* navigator = [TTNavigator navigator];
TTURLMap* map = navigator.URLMap;
[map from:@"*" toModalViewController:[TTWebController class]];

Everything works great, except that there is no "close" button on the TTWebController. I know this is usually pushed from the side and given a "back" button, but is there a way to add a "close" button without digging into the actual Three20 code?

Update:

Here is the code that I am using to create the text and then push the Modal View onto the screen. I want to dynamically send whichever URL is clicked, in place of "YourUrlHere".

CGRect frame = CGRectMake(100, 100, 200, 200);
TTStyledTextLabel* label = [[[TTStyledTextLabel alloc] initWithFrame:frame] autorelease];
NSString* labelText = @"http://www.yahoo.com http://www.google.com http://www.test.com";
label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES];
[self.view addSubview:label];   

TTNavigator* navigator = [TTNavigator navigator];
TTURLMap* map = navigator.URLMap;
myWebView *newViewController = [[myWebView alloc] initWithNibName:@"TTStyledTextLabelWebView" bundle:nil incomingURL:[NSString stringWithString:@"http://www.YourUrlHere.com"]];
[map from:@"*" toModalViewController:[newViewController class]];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2024-10-17 12:40:20

也许您真正想要的是您自己的视图控制器,它继承或包含 TTWebController。创建并映射到它而不是原始的 TTWebController,将按钮放在您喜欢的任何位置,并将内部的触摸连接到 [self DismissModalViewControllerAnimated:YES]; 这应该可以窍门。

编辑:

现在您有一个 myWebView 类型的自定义 Web 视图,我假设它继承自 TTWebController。那挺好的。顺便说一句,您实际上并不需要仅仅为了获取类而初始化它的新实例,您只需使用 [myWebView class] 即可做到这一点,因为类消息位于类上,而不是实例。

然而,即使您决定使用 xib 创建一个新实例,我也不认为 TTNavigator 会以同样的方式实例化您的控制器实例。相反,我相信它将使用此构造函数启动(从 TTWebController.m 中提取)

- (id)initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query {
  if (self = [self init]) {
    NSURLRequest* request = [query objectForKey:@"request"];
    if (request) {
      [self openRequest:request];
    } else {
      [self openURL:URL];
    }
  }
  return self;
}

因此,在您的子实现中,您可以提供此实现的自定义版本,如下所示:

- (id)initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query {
  if (self = [super initWithNavigatorURL:URL query:query]) {
    // code that creates a button and puts it somewhere
  }
  return self;
}

我还没有测试过它,我有兴趣看看什么如果它不这样做,它实际上正在做。如果您更喜欢 xibs(像我一样,不像 Three20),那么您需要执行额外的加载步骤。我的实用程序框架中有一个类别可以帮助解决此问题:

NSBundle+LoadNibView.h

#import <Foundation/Foundation.h>

@interface NSBundle(LoadNibView)

+ (id) loadNibView:(NSString*)className;

@end

NSBundle+LoadNibView.m

#import "NSBundle+LoadNibView.h"

@implementation NSBundle(LoadNibView)

+ (id) loadNibView:(NSString*)className
{
    return [[[NSBundle mainBundle] loadNibNamed:className owner:nil options:nil] objectAtIndex:0];
}

@end

objectAtIndex 部分是因为所有xibs 包含一个数组,即使里面只有一个视图。这将只返回 xib 中的第一个 UIView。

Probably what you actually want is your own view controller, that either inherits from or contains a TTWebController. Create and map to that instead of the raw TTWebController, put the button wherever you like and wire up the on touch up inside to [self dismissModalViewControllerAnimated:YES]; That should do the trick.

EDIT:

So now you have a custom web view of type myWebView that I am assuming inherits from TTWebController. That's good. As an aside, you don't really need to initialize a new instance of it just to get the class, you can do that with just [myWebView class] because the class message is on the class, not the instance.

However, even though you have decided to create a new instance using a xib, I don't think TTNavigator will be instantiating your controller instance in that same way. Instead I believe it will launch with this constructor (extracted from TTWebController.m)

- (id)initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query {
  if (self = [self init]) {
    NSURLRequest* request = [query objectForKey:@"request"];
    if (request) {
      [self openRequest:request];
    } else {
      [self openURL:URL];
    }
  }
  return self;
}

So, in your child implementation, you can provide a customized version of this implementation like so:

- (id)initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query {
  if (self = [super initWithNavigatorURL:URL query:query]) {
    // code that creates a button and puts it somewhere
  }
  return self;
}

I haven't tested it, I'd be interested in seeing what it actually is doing if it isn't doing that. If you prefer xibs (like me, unlike three20) then you'll need to do an extra step of loading that. I have a category in my utility framework that helps with this:

NSBundle+LoadNibView.h

#import <Foundation/Foundation.h>

@interface NSBundle(LoadNibView)

+ (id) loadNibView:(NSString*)className;

@end

NSBundle+LoadNibView.m

#import "NSBundle+LoadNibView.h"

@implementation NSBundle(LoadNibView)

+ (id) loadNibView:(NSString*)className
{
    return [[[NSBundle mainBundle] loadNibNamed:className owner:nil options:nil] objectAtIndex:0];
}

@end

The objectAtIndex part is because all xibs contain an array, even if there is only one view inside. This will just return the first UIView in the xib.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文