小编Adr*_*ian的帖子

如何在UIScrollView中加载UIViewController

这是我的设置.我有一个UIScrollView在我的主视图控制器的顶部,我加载了多个视图控制器.我还有一个Add按钮,它将使用Push segue呈现一个新的视图控制器.

我希望这个视图控制器也只在滚动视图的顶部而不是全屏加载.
到目前为止,我尝试了两种不同的东西,但没有任何工作:

  1. 在滚动视图中添加视图控制器prepareForSegue:

      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let addViewController = segue.destination as! AddViewController
    
        addChildViewController(addViewController)
        addViewController.view.frame = CGRect(x: 0, y: 0, width: width, height: height)
        scrollView.addSubview(addViewController.view)
        didMove(toParentViewController: self)
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在UIButton操作中添加视图控制器:

@IBAction func addDidTouch(_ sender:AnyObject){

    let addViewController = AddViewController()

    addChildViewController(addViewController)
    addViewController.view.frame = CGRect(x: 0, y: 0, width: width, height: height)
    scrollView.addSubview(addViewController.view)
    didMove(toParentViewController: self)
}
Run Code Online (Sandbox Code Playgroud)

这两个解决方案都崩溃了我的应用程序.
有没有办法正确实现这个?

ios swift

4
推荐指数
1
解决办法
2600
查看次数

如何将渐变添加到UIView作为扩展

我试图在Xcode中为视图添加一些渐变,为了简单起见,我尝试将我的方法添加为UIView的扩展:

extension UIView {
    func applyGradient() {
        let gradient = CAGradientLayer()
        gradient.colors = [UIColor(hex: "F5FF8C").cgColor,
                           UIColor(hex: "F1F99D").cgColor,
                           UIColor(hex: "FDFFE0").cgColor]
        gradient.locations = [0.0, 0.5, 1.0]

        self.layer.insertSublayer(gradient, at: 0)
    }
}
Run Code Online (Sandbox Code Playgroud)

但显然当我在我的电话中说这不起作用时viewDidLoad:

self.myView.applyGradient()
Run Code Online (Sandbox Code Playgroud)

有人能指出我做错了什么吗?

ios swift

4
推荐指数
1
解决办法
2274
查看次数

UITextfield文本颜色在不在焦点时不会改变

所以我有两个UITextFields:名字和金额和两个UIButtons:收入,费用.
当我按下费用按钮时,amount如果按下收入按钮,我希望我的文本字段颜色变为红色或绿色.

仅当amount文本字段处于焦点时才有效,如果name文本字段处于焦点,则颜色不会更改为金额.

有没有办法改变文本字段的颜色,如果它不在焦点?

编辑:

这是我改变颜色的代码:

@IBAction func typeBtnPressed(_ sender: UIButton) {
    if sender.tag == Buttons.expense.rawValue {
        amountTxt.textColor = .red
    } else {
        amountTxt.textColor = .green
    }
}
Run Code Online (Sandbox Code Playgroud)

ios

4
推荐指数
2
解决办法
3194
查看次数

在Java中混淆​​调用方法

class Parent
{
    private void method1()
    {
        System.out.println("Parent's method1()");
    }
    public void method2()
    {
        System.out.println("Parent's method2()");
        method1();
    }
}

class Child extends Parent
{
    public void method1()
    {
        System.out.println("Child's method1()");        
    }
}
class test {
    public static void main(String args[])
    {
        Parent p = new Child();
        p.method2();
    }
}
Run Code Online (Sandbox Code Playgroud)

我很困惑为什么在Parent :: method2()中调用method1()时它会调用Parents方法1()而不是Childs方法1?我看到只有当method1()是私有的时才会发生这种情况?有人能解释一下为什么吗?
谢谢.

java

3
推荐指数
2
解决办法
173
查看次数

在C++的枚举中重载输出运算符

它可以使枚举中的输出操作符过载吗?我遇到了各种错误(如果我使用类,我没有得到任何错误):

   ../src/oop.cpp:18:2: error: expected identifier before 'friend'
    ../src/oop.cpp:18:2: error: expected '}' before 'friend'
    ../src/oop.cpp:18:2: error: 'friend' used outside of class
    ../src/oop.cpp:18:16: error: expected initializer before '&' token
    ../src/oop.cpp:22:1: error: expected declaration before '}' token
Run Code Online (Sandbox Code Playgroud)

我要实现这样的东西(java代码):

public enum Type { 

  ACOUSTIC, ELECTRIC;

  public String toString() {
    switch(this) {
      case ACOUSTIC: return "acoustic";
      case ELECTRIC: return "electric";
      default:       return "unspecified";
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

编辑:

enum Type {
    //ACOUSTIC, ELECTRIC;
    inline std::ostream& operator << (std::ostream& os, Type t) // error here
    { …
Run Code Online (Sandbox Code Playgroud)

c++

3
推荐指数
1
解决办法
2650
查看次数

使用upcasting时的虚拟析构函数

每个人都说当至少有一个类方法是虚拟的时,析构函数应该是虚拟的.
我的问题是,在使用向上转换时说析构函数应该是虚拟的是不正确的?

class A {
public:

    ~A(){
        cout << "Destructor in A" << endl;
    }
};

class B: public A
{
public:

    ~B(){
        cout << "Destructor in B" << endl;
    }
};

int main()
{
    A* a = new B;
    cout << "Exiting main" << endl;
    delete a;
}
Run Code Online (Sandbox Code Playgroud)

我在这段代码中没有任何虚函数,但是如果我不使我的基础析构函数为虚拟,它就不会调用B析构函数.是的,我知道如果我没有虚函数,使用ucpasting是没有意义的.

谢谢.

c++ inheritance virtual-destructor

3
推荐指数
2
解决办法
648
查看次数

在XAML中为集合的项目设置Converter的位置

我刚刚将第一个转换器从int转换为string.我有一个带有整数的组合框填充(年),但如果值为0,我希望组合框显示"全部".

这是我的转换器:

public class IntToString : IValueConverter
{
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                int intY = (int)value;

                if (intY == 0)
                {
                    String strY = "All";
                    return strY;
                }
                else
                {
                    return intY.ToString();
                }
            }

            return String.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {

        }
    }
Run Code Online (Sandbox Code Playgroud)

在XAML中,我应该在哪里设置转换器?我在组合框的ItemsSource中尝试过:

ItemsSource="{Binding YearsCollection, Converter={StaticResource intToStringYearConverter}}"
Run Code Online (Sandbox Code Playgroud)

但我总是InvalidcastException这样做:

int intY = (int)value;
Run Code Online (Sandbox Code Playgroud)

c# wpf xaml converter

3
推荐指数
1
解决办法
3726
查看次数

如何使用蒙版为圆形图像添加边框

这是我的尝试:

func round() {
    let width = bounds.width < bounds.height ? bounds.width : bounds.height
    let mask = CAShapeLayer()
    mask.path = UIBezierPath(ovalInRect: CGRectMake(bounds.midX - width / 2, bounds.midY - width / 2, width, width)).CGPath

    self.layer.mask = mask

    // add border
    let frameLayer = CAShapeLayer()
    frameLayer.path = mask.path
    frameLayer.lineWidth = 4.0
    frameLayer.strokeColor = UIColor.whiteColor().CGColor
    frameLayer.fillColor = nil

    self.layer.addSublayer(frameLayer)
}
Run Code Online (Sandbox Code Playgroud)

它适用于iphone 6模拟器(故事板的大小为4.7),但在5s和6+上它看起来很奇怪:

在此输入图像描述

在此输入图像描述

这是一个自动布局问题吗?没有边框,自动布局工作正常.这是我第一次使用面具,所以我不确定我所做的是否正确.

round函数被调用viewDidLayoutSubviews.

有什么想法吗?

mask cashapelayer ios swift

3
推荐指数
1
解决办法
3589
查看次数

按值将向量返回到引用中

我有以下代码:

std::vector<Info*> filter(int direction)
{
    std::vector<Info*> new_buffer;
    for(std::vector<Info*>::iterator it=m_Buffer.begin();it<m_Buffer.end();it++)
    {
        if(((*it)->direction == direction)
        {
            new_buffer.push_back(*it);
        }
    }

    return new_buffer;
}

std::vector<Info*> &filteredInfo= filter(m_Direction);
Run Code Online (Sandbox Code Playgroud)

有人能解释一下这里发生了什么吗?过滤器方法是否会按值返回创建一个临时的,并且过滤后的信息永远不会被销毁,因为它的引用?

不确定我是否理解正确.在这种情况下,filteredInfo作为引用而不是一个引用之间的区别是什么?

c++

3
推荐指数
1
解决办法
592
查看次数

使用自定义框架时,"Interface Builder文件中的未知类BarChartView"出错

我想Charts framework在我的应用程序中使用它.我使用Carthage为此构建了二进制文件并添加到我的应用程序中并包含两个文件:

 - Charts-Swift.h
 - Charts.h
Run Code Online (Sandbox Code Playgroud)

然后我将视图的自定义类设置为BarChartView,但似乎XCode无法识别该类(无自动完成).并且还没有设置自定义模块.

当我运行应用程序时,我总是收到此警告:

Unknown class BarChartView in Interface Builder file
Run Code Online (Sandbox Code Playgroud)

我从互联网上尝试了一些解决方案,但似乎没有什么对我有用.
任何人都有解决方案Xcode 8.1Swift 3

xcode ios swift ios-charts

3
推荐指数
1
解决办法
1012
查看次数