小编Chr*_*att的帖子

Swift使用选择器参数,如闭包

我只是想知道是否可以将一个函数传递给一个按钮动作(通常是一个选择器).

例如,通常我会说:

UIBarButtonItem(title: "Press", style: .Done, target: self, action: "functionToCall")

func functionToCall() {
    // Do something
}
Run Code Online (Sandbox Code Playgroud)

但我想知道是否可以做以下事情:

UIBarButtonItem(title: "Press", style: .Done, target: self, action: {
    // Do Something
})
Run Code Online (Sandbox Code Playgroud)

我想要这样做的原因是因为我的功能非常简单,看起来它更整洁,更像Swift,因为它们强调它们放在封闭上.

closures ios swift

22
推荐指数
4
解决办法
5371
查看次数

Selenium和xpath:找到一个带有类/ id的div并验证里面的文本

我正在尝试xpath找到div并验证其中div有特定string的文本.

这是HTML:

<div class="Caption">
  Model saved
</div>
Run Code Online (Sandbox Code Playgroud)

<div id="alertLabel" class="gwt-HTML sfnStandardLeftMargin sfnStandardRightMargin sfnStandardTopMargin">
  Save to server successful
</div>
Run Code Online (Sandbox Code Playgroud)

这是我目前正在使用的代码:

viewerHelper_.getWebDriver().findElement(By.xpath("//div[contains(@class, 'Caption' and .//text()='Model saved']"));
viewerHelper_.getWebDriver().findElement(By.xpath("//div[@id='alertLabel'] and .//text()='Save to server successful']"));
Run Code Online (Sandbox Code Playgroud)

特别:

//div[contains(@class, 'Caption' and .//text()='Model saved']
//div[@id='alertLabel'] and .//text()='Save to server successful']
Run Code Online (Sandbox Code Playgroud)

java testing selenium xpath

18
推荐指数
2
解决办法
11万
查看次数

iOS防止计时器UILabel在数字改变时"摇动"

我有一个UILabel显示定时器输出的格式MM:ss:SS(分钟,秒,厘秒),然而它从左到右"摇动",因为厘秒的宽度改变 - 例如,"11"比"33"窄.

有什么方法可以缓解这个问题吗?我试过把它居中,给它一个固定的宽度,但它们似乎没有帮助.

ios autolayout swift adaptive-layout

9
推荐指数
2
解决办法
1649
查看次数

根据标签大小调整UICollectionViewCell的大小

我有一个UICollectionView带有不同大小标签的单元格.我正在尝试根据标签的大小调整单元格的大小.然而sizeForItemAtIndexPath,我创建单元格的地方似乎cellForItemAtIndexPath在我设置标签之前被调用..任何想法我能在这做什么?

- (void)getLabelSize:(UILabel *)label {
    float widthIs = [label.text boundingRectWithSize:label.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:label.font } context:nil].size.width;

    width = widthIs;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // Configure the cell
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    UILabel *label = (UILabel *)[cell viewWithTag:1000];
    label.text = @"This is pretty long";
    [label sizeToFit];
    [self getLabelSize:label];
    NSLog([NSString stringWithFormat:@"%f", width]);
    cell.backgroundColor = [UIColor whiteColor];

    return cell;   
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(width, 50);
}
Run Code Online (Sandbox Code Playgroud)

iphone objective-c ios uicollectionview

5
推荐指数
2
解决办法
7349
查看次数

Android 从 URI 读取文本文件

我有一个Uri指向来自 an 的文本文件intent,我正在尝试读取该文件以解析其中的字符串。这是我尝试过的,但失败了FileNotFoundException。该toString()方法似乎失去了/

java.io.FileNotFoundException: content:/com.google.android.apps.bigtop/attachments/downloads/528c4088144d1515d933ca406b7bc273/attachments/d_0_0_b562310a_52b6ec1c_c4d53f20d.txt 或 openNo.txt1800000000 文件失败

Uri data = getIntent().getData();
String text = data.toString();
if(data != null) {

    try {
       File f = new File(text);
       FileInputStream is = new FileInputStream(f); // Fails on this line
       int size = is.available();
       byte[] buffer = new byte[size];
       is.read(buffer);
       is.close();
       text = new String(buffer);
       Log.d("attachment: ", text);
    } catch (IOException e) {
        // TODO Auto-generated catch block
       e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

数据的价值是:

content://com.google.android.apps.bigtop/attachments/downloads/528c4088144d1515d933ca406b7bc273/attachments/d_0_0_b562310a_52b6ec1c_c4d5f0d3_73f7110d3_73f71289atxt%

而 …

java android text file android-intent

5
推荐指数
2
解决办法
6734
查看次数

天/小时/分钟/秒的快速倒数计时器标签

我正在创建一个倒数计时器,倒计时到一个NSDate集合UIDatePicker.我有一个标签,显示我们倒计时的日期,并且工作正常.

我还要添加的是剩余的整天数和当天剩余的小时/分钟/秒数(即从未超过23/59/59)的标签.这是我在那一刻所做的但它显然显示了整个倒计时的价值.希望有人可以帮助我在这里找出正确的逻辑.

let secondsLeft = sender.date.timeIntervalSinceDate(NSDate())
hoursLabel.text = String(secondsLeft % 3600)
minutesLabel.text = String((secondsLeft / 60) % 60)
secondsLabel.text = String(secondsLeft % 60)
Run Code Online (Sandbox Code Playgroud)

我想我正在寻找的是一些快速相当于datetime你在PHP中获得的类

datetime nsdate nstimer ios swift

5
推荐指数
2
解决办法
4187
查看次数

使用CIContext在GLKView中绘制时,Aspect填充AVCaptureVideoDataOutput

我正在从AVCaptureVideoDataOutputa中绘制相机输出GLKView,但相机是4:3,这与GLKView(全屏幕)的纵横比不匹配.我正试图获得一个方面填充,但相机输出似乎被压扁,以便它不会越过视图的边缘.如何在GLKView不弄乱纵横比的情况下使用全屏摄像机视图?

初始化视图:

videoDisplayView = GLKView(frame: superview.bounds, context: EAGLContext(api: .openGLES2))
videoDisplayView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
videoDisplayView.frame = superview.bounds
superview.addSubview(videoDisplayView)
superview.sendSubview(toBack: videoDisplayView)

renderContext = CIContext(eaglContext: videoDisplayView.context)
sessionQueue = DispatchQueue(label: "AVSessionQueue", attributes: [])

videoDisplayView.bindDrawable()
videoDisplayViewBounds = CGRect(x: 0, y: 0, width: videoDisplayView.drawableWidth, height: videoDisplayView.drawableHeight)
Run Code Online (Sandbox Code Playgroud)

初始化视频输出:

let videoOutput = AVCaptureVideoDataOutput()
videoOutput.setSampleBufferDelegate(self, queue: sessionQueue)
if captureSession.canAddOutput(videoOutput) {
    captureSession.addOutput(videoOutput)
}
Run Code Online (Sandbox Code Playgroud)

渲染输出:

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {

    // Need to shimmy this through type-hell …
Run Code Online (Sandbox Code Playgroud)

camera opengl-es ios avcapturesession swift

5
推荐指数
1
解决办法
885
查看次数

使用FragmentPagerAdapter以编程方式从Fragment更改选项卡

我正在尝试让我的应用从标签1更改为标签3.标签位于自定义TabsPagerAdapterextends FragmentPagerAdapter.

我试图更改标签喜欢这个,但它导致NullPointerException.机制是不同的FragmentPagerAdapter

TabHost host = (TabHost) getActivity().findViewById(android.R.id.tabhost);
host.setCurrentTab(2);
Run Code Online (Sandbox Code Playgroud)

java android android-tabhost android-fragments fragmentpageradapter

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

iOS 6中的iOS隐藏标签栏会创建黑条(iOS 6的修复会破坏iOS 7!)

我有一个标签式应用程序,在一个选项卡中有一个UIWebView.当我将设备旋转到横向时,我UIWebView在隐藏状态和标签栏的同时制作了全屏.

我已经在iOS 6中运行了 - 最初在旋转和隐藏标签栏时它会留下标签栏所在的黑色空间,所以fHeight代码修复了这个问题.但是,在iOS 6上它运行得很好,但现在它确实造成了iOS 6的黑条问题!! 有关解决方法的任何想法吗?

请看下面我的编辑

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
{
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        [self hideTabBar:self.tabBarController];
        [[UIApplication sharedApplication] setStatusBarHidden:TRUE withAnimation:UIStatusBarAnimationSlide];
    }
    else
    {
        [self showTabBar:self.tabBarController];
        [[UIApplication sharedApplication] setStatusBarHidden:FALSE withAnimation:UIStatusBarAnimationSlide];
    }
}

- (void) hideTabBar:(UITabBarController *) tabbarcontroller
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.5];
    float fHeight = screenRect.size.height;
    if(  UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
    {
        fHeight = screenRect.size.width;
    }

    for(UIView *view in self.tabBarController.view.subviews)
    {
        if([view …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c uiwebview ipad ios

2
推荐指数
1
解决办法
5921
查看次数

Android WebView - shouldOverrideUrlLoading 未通过 url 请求调用

我的 webapp 在加载过程中的某个时候向一个 url 发出请求,它是 callback:// 以便在我的 android 应用程序中触发一个函数。

我试图抓住这个请求,shouldOverrideUrlLoading但它没有被调用......有什么想法吗?

是非标准的 url 方案导致了这个吗?

java android webview

2
推荐指数
1
解决办法
4204
查看次数