我对Objective-C中的块使用有点困惑.我目前使用ARC,我的应用程序中有很多块,目前总是指代self
而不是弱引用.这可能是这些区块保留self
并防止被解除分配的原因吗?问题是,我应该总是使用块中的weak
引用self
吗?
-(void)handleNewerData:(NSArray *)arr
{
ProcessOperation *operation =
[[ProcessOperation alloc] initWithDataToProcess:arr
completion:^(NSMutableArray *rows) {
dispatch_async(dispatch_get_main_queue(), ^{
[self updateFeed:arr rows:rows];
});
}];
[dataProcessQueue addOperation:operation];
}
Run Code Online (Sandbox Code Playgroud)
ProcessOperation.h
@interface ProcessOperation : NSOperation
{
NSMutableArray *dataArr;
NSMutableArray *rowHeightsArr;
void (^callback)(NSMutableArray *rows);
}
Run Code Online (Sandbox Code Playgroud)
ProcessOperation.m
-(id)initWithDataToProcess:(NSArray *)data completion:(void (^)(NSMutableArray *rows))cb{
if(self =[super init]){
dataArr = [NSMutableArray arrayWithArray:data];
rowHeightsArr = [NSMutableArray new];
callback = cb;
}
return self;
}
- (void)main {
@autoreleasepool {
...
callback(rowHeightsArr);
}
}
Run Code Online (Sandbox Code Playgroud) iphone weak-references objective-c ios automatic-ref-counting
在我的自定义UIPageViewController
类中:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.model = [[BSTCMWelcomingPageViewModel alloc] init];
self.dataSource = self.model;
self.delegate = self;
self.pageControl = [UIPageControl appearance];
self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
self.pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
然后我以编程方式设置ViewController
按下按钮时的当前值:
- (void)scrollToNext
{
UIViewController *current = self.viewControllers[0];
NSInteger currentIndex = [self.model indexForViewController:current];
UIViewController *nextController = [self.model viewControllerForIndex:++currentIndex];
if (nextController) {
NSArray *viewControllers = @[nextController];
// This changes the View Controller, but PageControl doesn't update
[self setViewControllers:viewControllers …
Run Code Online (Sandbox Code Playgroud) 我花了将近8个小时来了解如何跳转到UIPageViewController中的特定页码...下面是我的项目看起来像
我想制作一个看起来像Ibooks的应用---
我已经从这里提供的代码中获得了帮助 - http://www.ioslearner.com/tag/uipageviewcontroller-sample-code/
我已经制作了一个plist和UITableView,我从TableView中选择了值并在UIPAgeiewController上放置的webView上显示相同,但问题是只有Web视图中的页面发生了变化,而不是UIPageViewController的实际页面编号....
如果我缩小我的问题,它看起来像---是否有办法在UIPageViewController中切换页码....
我们设置了左右按钮,供用户快速浏览不同的汽车.如果用户快速点击下一页10次或更多次,我们的页面视图控制器将丢失视图控制器.
这是车辆正确显示的车辆页面(模糊以隐藏不相关的信息).见图:
如果滚动动画打开(true),则在快速点击右箭头6次或更多次后会丢失车辆页面.见图:
码:
private func show(viewController:UIViewController, going direction: UIPageViewControllerNavigationDirection) {
let viewControllers = [viewController]
let isAnimated = true // false always works. However, animation is required.
setViewControllers(viewControllers, direction: direction, animated: isAnimated, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
在调试时以及当页面视图控制器停止显示汽车时,我确保正在设置的视图控制器不是零并且列表(汽车)也是非零的.
我尝试了UIPageViewController的解决方案的变体,如何正确跳转到特定页面而不会弄乱数据源指定的顺序?使用完成块的位置.但是,它没有用.
weak var pvcw: UIPageViewController? = self
setViewControllers(viewControllers, direction: direction, animated: true, completion: {(_ finished: Bool) -> Void in
let pvcs: UIPageViewController? = pvcw
if pvcs == nil {
return
}
DispatchQueue.main.async(execute: {() -> Void in
pvcs?.setViewControllers(viewControllers, direction: direction, animated: …
Run Code Online (Sandbox Code Playgroud) 我有一个包含多个页面的UIPageViewController,并且在它们之间滑动时,一切正常。PageViewController自动预加载当前页面的相邻页面,以确保平滑滚动(我有UIPageViewControllerTransitionStyle.Scroll
)
另外,我还可以通过输入数字跳到特定页面。问题是,当我以编程方式跳转到某个页面时,PageViewController似乎还没有预加载相邻页面,因此当我从刚跳转到的页面开始滑动时,会有半秒的延迟, PageViewController加载下一个ViewController,以便可以显示它。
我用这个答案跳到页面:https : //stackoverflow.com/a/18602186/5461994
并将其转移到Swift:
weak var pvcw = pvc
pvc.setViewControllers([page], direction: UIPageViewControllerNavigationDirection.Forward, animated: true) { finished in
if let pvcs = pvcw {
dispatch_async(dispatch_get_main_queue(), {
pvcs.setViewControllers([page], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
})
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何获取UIPageViewController来预加载刚跳转到的页面的相邻页面,就像它在刷卡过程中自动执行的一样?
PS:我在启动应用程序时遇到同样的问题。当我初始化PageViewController并设置第一个可见页面时,如下所示:
pvc.setViewControllers([firstPage], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
Run Code Online (Sandbox Code Playgroud)
第一次滑动到第二页时,我也有这种延迟。任何帮助表示赞赏!
基本上我想移动我的 PageViewController 不仅用滑动而且用按钮。
@IBAction func nextButtonAction(_ sender: Any){
}
Run Code Online (Sandbox Code Playgroud)
我的 PageViewController 在本指南中看起来像
我拥有的:
对不起,如果它是重复的,没有发现完全相同的情况
试图在每个 ViewController 上调用 UIPageViewControllerDataSource 但也没有用我认为这不是最好的方法
编辑: 这是我的 PageViewController
class PageVC: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
lazy var VCarr: [UIViewController] = {
return [self.VCInstance(name: "signUpVerification"),
self.VCInstance(name: "signUpVerification1"),
self.VCInstance(name: "signUpVerification2"),
]
}()
func VCInstance(name:String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier:name)
}
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
if let firsVC = VCarr.first {
setViewControllers([firsVC], direction: .forward, animated: true, completion: …
Run Code Online (Sandbox Code Playgroud)