我正在尝试保持我的视图控制器清洁,如本文所述objc.io问题#1更轻的视图控制器.我在Objective-C中测试了这个方法,它工作正常.我有一个单独的类来实现UITableViewDataSource
方法.
#import "TableDataSource.h"
@interface TableDataSource()
@property (nonatomic, strong) NSArray *items;
@property (nonatomic, strong) NSString *cellIdentifier;
@end
@implementation TableDataSource
- (id)initWithItems:(NSArray *)items cellIdentifier:(NSString *)cellIdentifier {
self = [super init];
if (self) {
self.items = items;
self.cellIdentifier = cellIdentifier;
}
return self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
cell.textLabel.text = self.items[indexPath.row];
return cell;
}
@end
Run Code Online (Sandbox Code Playgroud)
从tableview控制器,我所要做的就是实例化这个类的一个实例并将其设置为tableview的数据源并且它完美地工作.
self.dataSource = [[TableDataSource alloc] initWithItems:@[@"One", @"Two", …
Run Code Online (Sandbox Code Playgroud) 我正在尝试通过单击swift中的按钮来更改包含图像的uiimage控件.为此,我写了一个简单的"猜我有多少手指"应用程序.我已经阅读了一些stackoverflow文章,但无法解决问题.下面你可以看到我的代码和我的ui.如何在点击时更改更改图像?
这是我的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet var myImageView: UIImageView!
@IBOutlet var inputField: UITextField!
@IBAction func clickedGuessButtonAction(sender: AnyObject) {
println("Guess button clicked")
var randomX = Int(arc4random()%6)
println("randomX = \(randomX)")
var guess = inputField.text.toInt();
let image0 = UIImage(named: "images/qm.jpg")
let image1 = UIImage(named: "images/tick.jpg")
let image2 = UIImage(named: "images/cross.jpg")
if((inputField.text) != nil){
if(guess == randomX){
println("correct")
myImageView.image=UIImage(named: "tick.jpg") // THIS IS NOT WORKING
myImageView.image=image1 // THIS IS NOT WORKING TOO
inputField.resignFirstResponder();// hides keyboard
}
else
{
println("wrong")
myImageView.image=UIImage(named: "cross.jpg") …
Run Code Online (Sandbox Code Playgroud) 我有一个自定义UITableViewCell
子类及其相关的xib.我在这个单元格中有一个UILabel
和一个UIButton
,我已经将按钮内部的触摸连接到子类.
我需要的是点击单元格中的按钮,以获取具有该按钮的单元格的索引路径.也许可以通过委托或其他东西将其发送回视图控制器.
因为我是里面UITableViewCell
的子类,我不能使用的溶液等这个,因为我不从小区子类中,必须在tableview中的参考.经过进一步调查,我找到了另一个解决方案,我在Swift中实现了这个.
import UIKit
class ContactCell: UITableViewCell {
@IBOutlet weak var nameLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
selectionStyle = .None
}
@IBAction func callButtonPressed(sender: UIButton) {
let indexPath = (self.superview as UITableView).indexPathForCell(self)
println("indexPath?.row")
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我点击按钮时,它会崩溃,并显示一条错误消息,指出Swift动态转换失败.
知道我的代码有什么问题吗?
或者我愿意接受任何其他建议,这些建议可以让我以任何其他方式达到预期的结果.
谢谢.
我正在尝试MFMailComposeViewController
在应用程序中显示。
if MFMailComposeViewController.canSendMail() {
let mailComposeViewController = MFMailComposeViewController()
mailComposeViewController.navigationBar.tintColor = .white
mailComposeViewController.mailComposeDelegate = self
mailComposeViewController.setToRecipients(["support@gmail.com"])
mailComposeViewController.setSubject("Feedback")
present(mailComposeViewController, animated: true)
} else {
print("This device is not configured to send email. Please set up an email account.")
}
Run Code Online (Sandbox Code Playgroud)
在 iOS 12 中,它显示没有问题。在模拟器和设备中。
但是当我在运行 iOS 13 的设备上运行相同的项目时,它看起来像这样。
导航栏颜色不见了。此外,发送按钮也是不可见的。
所以我添加了mailComposeViewController.navigationBar.backgroundColor = .mv_primary
但它仍然没有显示在设备上。奇怪的是,背景颜色显示在模拟器中。
然而,有一个奇怪的行为。该MFMailComposeViewController
立即本身驳回时,我在模拟器中运行它。
以下错误也显示在 Xcode 控制台中。
[常见] [FBSSystemService][0x5f27] 处理 com.apple.MailCompositionService 的打开请求时出错:{ userInfo = { FBSOpenApplicationRequestID = 0x5f27; } 底层错误 = ; } 2019-11-01 14:40:05.214158+0530 MailCompose[11289:262267] [Assert] 连接请求在没有恢复我们的 …
我main()
在一些我遇到的JavaScript文件中看到了一个函数.它是否与您在其他语言中使用的主要功能相同,例如C#,C++?如果在JS文件中放置一个main函数,代码是否开始执行?或者它只是用于功能的另一个名称?
我在网上搜索过,但没有找到任何关于此事的有用信息.
我已经UIView
在视图控制器上添加了一个IB.我们称之为redView
.
然后我在代码中使用自动布局约束固定了它的所有四个边,当我运行它时,它看起来像预期的那样.
现在我想以UILabel
编程方式向该视图添加一个并使用自动布局约束将其定位在中心.
下面是我到目前为止的代码.
import UIKit
class ViewController: UIViewController {
@IBOutlet private var redView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
redView.setTranslatesAutoresizingMaskIntoConstraints(false)
let leadingConstraint = NSLayoutConstraint(item: redView, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: redView, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .Trailing, multiplier: 1, constant: 0)
let topConstraint = NSLayoutConstraint(item: redView, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: 0)
let bottomConstraint …
Run Code Online (Sandbox Code Playgroud) 首先,在SO上有一个相同标题的问题,但它不是我正在寻找的,也没有完整的答案.
所以这是我的问题.假设我有这个指向图像的URL.
https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/299595_10150290138650735_543370734_8021370_355110168_n.jpg
Run Code Online (Sandbox Code Playgroud)
一旦我将此参数放在?dl=1
URL的末尾,它就可以下载了.
https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/299595_10150290138650735_543370734_8021370_355110168_n.jpg?dl=1
Run Code Online (Sandbox Code Playgroud)
我正在尝试通过用户脚本执行此任务.所以我使用了XMLHttpRequest.
var url = "https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/299595_10150290138650735_543370734_8021370_355110168_n.jpg?dl=1";
var request = new XMLHttpRequest();
request.open("GET", url, false);
request.send(null);
if (request.status === 200)
{
alert(request.statusText);
}
Run Code Online (Sandbox Code Playgroud)
这是一个小提琴.
但它不起作用.
我按照本教程学习如何构建可以扫描QR码的Android应用程序.
这是完整的代码.我使用等级添加了Google Play服务compile 'com.google.android.gms:play-services:7.8.0'
.
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bitinvent.io.qrscanner" >
<meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="barcode"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Run Code Online (Sandbox Code Playgroud)
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<SurfaceView
android:id="@+id/cameraView"
android:layout_width="640px"
android:layout_height="480px"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"/>
<TextView
android:id="@+id/infoTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/cameraView"
android:layout_marginLeft="16dp"
android:text="Nothing to read"
android:textSize="20sp"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
MainActivity.java
package bitinvent.io.qrscanner;
import …
Run Code Online (Sandbox Code Playgroud) 我正在UISegmentedControl
以编程方式将导航栏添加到应用的位置titleView
.但正如Apple文档提到的那样titleView
,如果leftBarButtonItem不是nil,则忽略此属性.
但我也想要后退按钮.就像他们在自己的图像中说明的那样!
下面是我添加的代码UISegmentedControl
.
self.navigationItem.leftBarButtonItem = nil;
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Filter_Personnal", @"Filter_Department", @"Filter_Company", nil];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
self.navigationItem.titleView = statFilter;
Run Code Online (Sandbox Code Playgroud)
还有另一种方法可以添加一个UISegmentedControl
后退按钮吗?
谢谢.
objective-c uinavigationbar uisegmentedcontrol uinavigationcontroller ios
ios ×6
swift ×5
android ×2
javascript ×2
objective-c ×2
uitableview ×2
autolayout ×1
database ×1
datasource ×1
download ×1
ios13 ×1
java ×1
mfmailcomposeviewcontroller ×1
nsindexpath ×1
qr-code ×1
sqlite ×1
uibutton ×1
uiimage ×1
uilabel ×1
uiview ×1
userscripts ×1
xcode11 ×1