Objective c 目标C:Tabbaritem-Tabbaritem-Tabbaritem->;方法调用->;但是WebView没有刷新 努力实现

Objective c 目标C:Tabbaritem-Tabbaritem-Tabbaritem->;方法调用->;但是WebView没有刷新 努力实现,objective-c,uitabbarcontroller,uitabbar,uitabbaritem,Objective C,Uitabbarcontroller,Uitabbar,Uitabbaritem,当我点击tabbaritem(如#2)时,它将调用该方法并重新加载web视图 问题 当我点击tabbaritem时,该方法被调用,但web视图没有重新加载 问题: 如果我在VC本身上调用该方法。我可以设法重新加载web视图。只有在点击tabbaritem时调用它,它才不会重新加载web视图 代码 MyTabBarController.m 班级 -(void)加载类{ sURL=@“www.share-fitness.com/apps/class.asp?memCode=SF100012&dtp

当我点击tabbaritem(如#2)时,它将调用该方法并重新加载web视图

问题 当我点击tabbaritem时,该方法被调用,但web视图没有重新加载

问题: 如果我在
VC
本身上调用该方法。我可以设法重新加载web视图。只有在点击tabbaritem时调用它,它才不会重新加载web视图

代码 MyTabBarController.m 班级
-(void)加载类{
sURL=@“www.share-fitness.com/apps/class.asp?memCode=SF100012&dtpClass=13/09/2018&lang=EN&lat=37.785835&long=-122.406418&ver=1&plat=IOS”
NSLog(@“要加载的URL%@”,sURL);
NSURL*url=[NSURL URLWithString:sURL];
sRefresh=sURL;
[[NSURLCache sharedURLCache]移除所有缓存响应];
NSURLRequest*urlRequest=[nsurlRequestRequestWithURL:url];
[webView loadRequest:urlRequest];
[webView setDelegate:(id)self];
UIRefreshControl*refreshControl=[[UIRefreshControl alloc]init];
[refreshControl addTarget:self action:@selector(HandlerRefresh:)for ControlEvents:UIControlEventValueChanged];
[webView.scrollView添加子视图:刷新控件];
}

正如我在另一个回复中提到的,我认为每次选择选项卡栏时都从服务器刷新视图不是一种好的用户体验(对于用户来说,每次等待服务器刷新数据都会非常烦人)

也就是说,您发布的代码的问题是您正在初始化TabBarControllerDelegate方法中类的新实例,因此该方法将在该新实例上调用,而不是在TabBarController的视图控制器中显示/存在的实例上调用。具体而言,这两行正在初始化新实例:

[[[Classes alloc] init] LoadClasses];
[[[Gym alloc] init] handleRefreshGym:nil];
相反,您应该查找已经存在的实例,并对其调用方法

我建议创建一个带有公共方法的
ParentViewController
,其行为
-(void)dostuffwhentabarcontroller选择(只是示例命名,以明确它对您的影响),然后让您希望的每个视图控制器在被选择为此父级的子类时执行某些操作(并且有自己的
-(void)dostuffWhenTabbarController selects;
)。通过这种方式,在TabBarController的委托方法中,您只需找到
ParentViewController
(与所选视图控制器关联)的适当实例,并在TabBarController选择时调用
-(void)dostuff方法

这里有一个例子来说明我的意思:

ParentViewController.h:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ParentViewController : UIViewController
- (void)doStuffWhenTabBarControllerSelects;
@end

NS_ASSUME_NONNULL_END
#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@interface FirstViewController : ParentViewController


@end
#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@interface SecondViewController : ParentViewController


@end
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyTabBarController : UITabBarController <UITabBarControllerDelegate>

@end

NS_ASSUME_NONNULL_END
FirstViewController.h:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ParentViewController : UIViewController
- (void)doStuffWhenTabBarControllerSelects;
@end

NS_ASSUME_NONNULL_END
#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@interface FirstViewController : ParentViewController


@end
#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@interface SecondViewController : ParentViewController


@end
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyTabBarController : UITabBarController <UITabBarControllerDelegate>

@end

NS_ASSUME_NONNULL_END
SecondViewController.h:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ParentViewController : UIViewController
- (void)doStuffWhenTabBarControllerSelects;
@end

NS_ASSUME_NONNULL_END
#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@interface FirstViewController : ParentViewController


@end
#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@interface SecondViewController : ParentViewController


@end
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyTabBarController : UITabBarController <UITabBarControllerDelegate>

@end

NS_ASSUME_NONNULL_END
MyTabBarController.h:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ParentViewController : UIViewController
- (void)doStuffWhenTabBarControllerSelects;
@end

NS_ASSUME_NONNULL_END
#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@interface FirstViewController : ParentViewController


@end
#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@interface SecondViewController : ParentViewController


@end
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyTabBarController : UITabBarController <UITabBarControllerDelegate>

@end

NS_ASSUME_NONNULL_END
然后,当您在两个选项卡之间进行选择时,您将看到以下输出:

I'm doing stuff on the FirstViewController when the tab bar controller delegate calls back to selection

I'm doing stuff on the SecondViewController when the tab bar controller delegate calls back to selection
我建议做一些额外的研究/阅读文档:

这里有大量的初学者信息:

UITABBARC控制器:

UITabBarControllerDelegate:

另一个有用的提示是,在Xcode中,您可以按住option键并单击某些内容,以快速查看说明/文档

您也可以右键单击某个内容并“跳转到定义”。苹果的大多数实现都会在标题中包含附加信息

以下是UITabBarController标题中的示例:

/*!
 UITabBarController manages a button bar and transition view, for an application with multiple top-level modes.

 To use in your application, add its view to the view hierarchy, then add top-level view controllers in order.
 Most clients will not need to subclass UITabBarController.

 If more than five view controllers are added to a tab bar controller, only the first four will display.
 The rest will be accessible under an automatically generated More item.

 UITabBarController is rotatable if all of its view controllers are rotatable.
 */

NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarController : UIViewController <UITabBarDelegate, NSCoding>
/*!
UITabBarController为具有多种顶级模式的应用程序管理按钮栏和转换视图。
要在应用程序中使用,请将其视图添加到视图层次结构中,然后依次添加顶级视图控制器。
大多数客户机不需要将UITabBarController子类化。
如果向选项卡栏控制器添加了五个以上的视图控制器,则仅显示前四个。
其余部分将在自动生成的“更多”项下进行访问。
如果UITabBarController的所有视图控制器都是可旋转的,则UITabBarController是可旋转的。
*/
NS\u类\u可用\u IOS(2\u 0)@接口UITabBarController:UIViewController

在“帮助”菜单下还有“开发者文档”(CMD+SHIFT+0),其中包含大量有用的信息。

正如我在另一个回复中提到的,我认为每次选择选项卡栏时从服务器刷新视图不是一种好的用户体验(用户每次都要等待服务器刷新数据,这会让人非常恼火)

也就是说,您发布的代码的问题是,您正在初始化TabBarControllerDelegate方法中类的新实例,因此该方法将在该新实例上调用,而不是在TabBarController的视图控制器中显示/存在的实例上调用。具体而言,这两行正在初始化新实例实例:

[[[Classes alloc] init] LoadClasses];
[[[Gym alloc] init] handleRefreshGym:nil];
相反,您应该查找已经存在的实例,并对其调用方法

我建议创建一个带有公共方法的
ParentViewController
,其行为
-(void)dostuff whentabarcontrollerselects;
(仅举一个例子,以明确它对您的影响),然后让您希望拥有的每个视图控制器在被选为此父类的子类时执行某些操作(并有自己的
-(void)dostuff实现,当TabBarController选择时;
)。通过这种方式,在TabBarController的委托方法中,您只需找到
父视图控制器
(与所选视图控制器关联)的适当实例并调用
-(void)DostuffWhenTabbarController选择;
方法

这里有一个例子来说明我的意思:

ParentViewController.h:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ParentViewController : UIViewController
- (void)doStuffWhenTabBarControllerSelects;
@end

NS_ASSUME_NONNULL_END
#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@interface FirstViewController : ParentViewController


@end
#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@interface SecondViewController : ParentViewController


@end
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyTabBarController : UITabBarController <UITabBarControllerDelegate>

@end

NS_ASSUME_NONNULL_END
FirstViewController.h:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ParentViewController : UIViewController
- (void)doStuffWhenTabBarControllerSelects;
@end

NS_ASSUME_NONNULL_END
#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@interface FirstViewController : ParentViewController


@end
#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@interface SecondViewController : ParentViewController


@end
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyTabBarController : UITabBarController <UITabBarControllerDelegate>

@end

NS_ASSUME_NONNULL_END
SecondViewController.h:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ParentViewController : UIViewController
- (void)doStuffWhenTabBarControllerSelects;
@end

NS_ASSUME_NONNULL_END
#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@interface FirstViewController : ParentViewController


@end
#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@interface SecondViewController : ParentViewController


@end
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyTabBarController : UITabBarController <UITabBarControllerDelegate>

@end

NS_ASSUME_NONNULL_END
MyTabBarController.h:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ParentViewController : UIViewController
- (void)doStuffWhenTabBarControllerSelects;
@end

NS_ASSUME_NONNULL_END
#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@interface FirstViewController : ParentViewController


@end
#import <UIKit/UIKit.h>
#import "ParentViewController.h"

@interface SecondViewController : ParentViewController


@end
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MyTabBarController : UITabBarController <UITabBarControllerDelegate>

@end

NS_ASSUME_NONNULL_END
然后,当您在两个选项卡之间进行选择时,您将看到以下输出:

I'm doing stuff on the FirstViewController when the tab bar controller delegate calls back to selection

I'm doing stuff on the SecondViewController when the tab bar controller delegate calls back to selection
我会记录的