gpt4 book ai didi

ios - 将方法从一个类覆盖到另一个类

转载 作者:行者123 更新时间:2023-11-30 13:33:12 24 4
gpt4 key购买 nike

有一个scrollView和两个附加到scrollView的 View Controller 现在我想访问从 View Controller 类之一到包含 ScrollView 的类的方法。所以我可以使用方法scrollViewDidEndDecleating。如果我在同一个类中尝试 scrollViewDidEndDecleating ,它将不会执行。但如果我尝试重写该方法。我收到一个 fatal error 。

包含 ScrollView 的类的代码

import UIKit

class ViewController: UIViewController , UIScrollViewDelegate {

@IBOutlet weak var scrollVieww: UIScrollView!

var mainRead : Bool = false

override func viewDidLoad() {
super.viewDidLoad()
self.scrollVieww.pagingEnabled = true
self.scrollVieww.delegate = self

// Do any additional setup after loading the view, typically from a nib.
let V1 = self.storyboard?.instantiateViewControllerWithIdentifier("HomeScreen") as UIViewController!
//Add initialized view to main view and its scroll view and also set bounds
self.addChildViewController(V1)
self.scrollVieww.addSubview(V1.view)
V1.didMoveToParentViewController(self)
V1.view.frame = scrollVieww.bounds

//Initialize using Unique ID for the View
let V2 = self.storyboard?.instantiateViewControllerWithIdentifier("MainScreen") as UIViewController!
//Add initialized view to main view and its scroll view also set bounds
self.addChildViewController(V2)
self.scrollVieww.addSubview(V2.view)
V2.didMoveToParentViewController(self)
V2.view.frame = scrollVieww.bounds

//Create frame for the view and define its urigin point with respect to View 1
var V2Frame: CGRect = V2.view.frame
V2Frame.origin.x = self.view.frame.width
V2.view.frame = V2Frame

//The width is set here as we are dealing with Horizontal Scroll
//The Width is x3 as there are 3 sub views in all
self.scrollVieww.contentSize = CGSizeMake((self.view.frame.width) * 2, (self.view.frame.height))
}

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
if(scrollView.contentOffset.x == self.view.frame.size.width * 1 && !mainRead){
mainRead = true
// if i execute this i get a fatal error in the animate method in the class Main_Screen
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let vc = storyboard.instantiateViewControllerWithIdentifier("MainScreen") as? Main_Screen {
vc.animate()
}
}
}

现在是 Home_Screen 的代码

import UIKit

public class Main_Screen: UIViewController , UIScrollViewDelegate {

@IBOutlet weak var aboutMebtn: UIButton!
@IBOutlet weak var studybtn: UIButton!
@IBOutlet weak var appsbtn: UIButton!
@IBOutlet weak var skillsbtn: UIButton!

var scrollView : UIScrollView!

@IBOutlet weak var avatarImageView: UIImageView!

weak var pageControl: UIPageControl!

var mainRead : Bool = false

override public func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
// Making the view round
for v : UIView in self.view.subviews {
if v.tag != 1 {
v.layer.cornerRadius = v.frame.size.width/2
v.layer.masksToBounds = true
}
}
}

public func animate(){
// I get a fatal error here
var buttons : Array<UIButton> = [aboutMebtn , studybtn , appsbtn , skillsbtn]

avatarImageView.alpha = 0.0
avatarImageView.transform = CGAffineTransformMakeScale(1.25, 1.25)

UIView.animateWithDuration(1.0, delay: 1.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: .CurveEaseIn, animations: { () -> Void in
self.avatarImageView.alpha = 1.0
self.avatarImageView.transform = CGAffineTransformMakeScale(0.75, 0.75)
}, completion: nil)
}

我无法使用 viewDidAppear 方法的 viewWillAppear,因为这是 storyboard 中的第二个 viewController

最佳答案

线路

storyboard.instantiateViewControllerWithIdentifier("MainScreen") as? Main_Screen

正在创建 View Controller 的新实例,未找到已存在的 View Controller 。

如果您需要在同级 ViewController 之间进行通信,则需要从其父 ViewController 设置连接。

关于ios - 将方法从一个类覆盖到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36338530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com