使用不带UITabBarController的UITabBar来控制UIWebView [英] Use a UITabBar without UITabBarController to control a UIWebView

查看:117
本文介绍了使用不带UITabBarController的UITabBar来控制UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 UITabBar 让用户导航一个 UIWebView 。这是可能的还是我会更好地使用 UIToolbar ?如果最好使用 UIToolbar ,我将如何模拟按钮的选定视觉状态,方式 UITabBar 本地呢?

I want to use a UITabBar to let the user navigate a single UIWebView. Is this possible or would I be better off using a UIToolbar? If better off using a UIToolbar, how would I simulate the "selected" visual state of a button, the way UITabBar natively does?

推荐答案

我提供了Objective-C以及Swift版本来解决这个问题。您真正需要做的唯一事情是在类上实现UITabBarDelegate协议。它不一定是ViewController,但在这个简单的例子中它是)。

UITabBar without UITabBarController to control UIWebView

I have supplied the Objective-C as well as the Swift version to solve this problem. The only thing you really need to do is to implement the UITabBarDelegate protocol on a class. It doesn't have to be ViewController but in this simple example it is).

在这个Objective-C示例中,您可以在UITabBar中切换三个搜索引擎。

In this Objective-C example you can switch between three search engines from the UITabBar.

首先,您需要设置视图,如下所示:
(除非先将UITabBarDelegate添加到ViewController,否则无法设置代理插座,请参阅代码)

First you have to set up your views, as follows: (You cannot set the delegate outlet unless you add the UITabBarDelegate to the ViewController first, see code)

现在因为您没有直接引用UITabBar中的项目,因为我们在Interface Builder中设置了所有内容,您需要在它们上设置标记。我不是这种方法的忠实粉丝但如果你注意它就可以了。如果你想用动态加载按钮设置一些更复杂的东西,你应该考虑将UITabBar子类化。

Now because you don't have a direct reference to the items inside of the UITabBar because we set it all up in Interface Builder you need to set the tags on them. I'm not a huge fan of this method but if you take care it works just fine. If you want to set up something more complicated with dynamic loading buttons you should consider subclassing UITabBar.

现在重要的是右边的标记设置。您需要为每个按钮设置一个数字。在这种情况下,我将它们设置为0,1和2,因为它或多或少与数组的计数有关,但您可以自由选择任何数字。

Now the important thing here is that "tag" setting on the right. You need to set that up to a number for each of the buttons there. In this case I have set them to 0, 1 and 2 as that more or less correlates to how an array counts but you're free to choose any number.

拥有设置这个我们可以看看代码:

Having set up this we can take a look at the code:

.h文件:

#import <UIKit/UIKit.h>

@interface LVDViewController : UIViewController<UITabBarDelegate> 
// implementing the delegate allows you to drag and drop the delegate reference from the first screenshot

@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UITabBar *tabBar;

@end

.m文件:

#import "LVDViewController.h"

@interface LVDViewController ()

@end

@implementation LVDViewController

- (void)viewDidLoad;
{
    [super viewDidLoad];
    // Set the first item as selected. It will automatically load Google
    [self tabBar:self.tabBar didSelectItem:[self.tabBar.items firstObject]];
    self.tabBar.selectedItem = [self.tabBar.items firstObject];
    // This is optional
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
{
    NSString *searchEngineURLString = nil;
    switch (item.tag)
    {
        case 0:
            searchEngineURLString = @"https://google.com";
            break;
        case 1:
            searchEngineURLString = @"https://duckduckgo.com";
            break;
        case 2:
            searchEngineURLString = @"https://bing.com";
            break;
    }

    [self loadSearchEngine:searchEngineURLString];
}

- (void)loadSearchEngine:(NSString *)searchEngineURLString;
{
    NSURL *searchEngineURL = [NSURL URLWithString:searchEngineURLString];
    NSURLRequest *searchEngineRequest = [NSURLRequest requestWithURL:searchEngineURL];
    [self.webView loadRequest:searchEngineRequest];
}

@end






Xcode 6中的Swift解决方案



现在我们有了Objective-C版本的Swift版本。首先我们再次在Interface Builder中设置View,这次Bing是最受欢迎的搜索引擎,而不是Google,以反映新的Apple观点。它几乎完全相同,您还必须再次将标签添加到标签栏项目。


Swift solution in Xcode 6

Now we have the Objective-C version working it's time for the Swift version of this. First we setup our View in Interface Builder again, this time Bing is the favorite search engine instead of Google to reflect the new Apple viewpoint. It's almost identical and you also have to add the tags to the tab bar items again.

我们再次使用View Controller,但现在我们使用的是Swift,因此它只有一个文件:

We have our View Controller again, but now we are using Swift so it's only one file:

import UIKit

class ViewController: UIViewController, UITabBarDelegate {
    @IBOutlet var tabBar : UITabBar
    @IBOutlet var webView : UIWebView

    override func viewDidLoad() {
        super.viewDidLoad()

        loadSearchEngine("https://www.bing.com")
        tabBar.selectedItem = tabBar.items[0] as UITabBarItem;
    }

    func tabBar(tabBar: UITabBar!, didSelectItem item: UITabBarItem!) {
        var searchEngineURLString: NSString! = "";

        switch item.tag  {
        case 0:
            searchEngineURLString = "https://www.bing.com"
            break
        case 1:
            searchEngineURLString = "https://www.duckduckgo.com"
            break
        case 2:
            searchEngineURLString = "https://www.google.com"
            break
        default:
            searchEngineURLString = "https://www.bing.com"
            break
        }

        loadSearchEngine(searchEngineURLString);
    }

    func loadSearchEngine(searchEngineURLString: NSString!) {
        var searchEngineURL = NSURL(string: searchEngineURLString)
        var searchEngineURLRequest = NSURLRequest(URL: searchEngineURL)
        webView.loadRequest(searchEngineURLRequest)
    }
}

这篇关于使用不带UITabBarController的UITabBar来控制UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆