我无法将init方法添加到以下UIViewController类.我需要在init方法中编写一些代码.我必须编写init(编码器)方法吗?即使我添加编码器和解码器方法,我仍然会遇到错误.我也尝试使用没有任何参数的init方法,但这似乎也不起作用.
class ViewController: UIViewController {
var tap: UITapGestureRecognizer?
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nil, bundle: nil)
tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
}
...
...
}
Run Code Online (Sandbox Code Playgroud)
如果我调用不带参数的super.init()方法,则错误为"必须调用超类的指定初始化程序",如果我传递参数nib和bundle,则错误为"必需的初始化程序初始化程序(编码器)".
即使我添加init(编码器)和init(解码器)它也不起作用.
为什么我可以做到这一点没有任何错误:
var testDto = ModelDto(modelId: 1)
testDto.objectId = 2
Run Code Online (Sandbox Code Playgroud)
虽然我定义了这个:
protocol DataTransferObject {
var objectType: DtoType { get }
var parentObjectId: Int { get set }
var objectId: Int { get }
var objectName: String { get set }
}
struct ModelDto: DataTransferObject {
var objectType: DtoType
var parentObjectId: Int
var objectId: Int
var objectName: String
init(modelId: Int) {
self.objectType = DtoType.Model
self.objectId = modelId
self.parentObjectId = -1
self.objectName = String()
}
}
Run Code Online (Sandbox Code Playgroud)
如果我的协议中的定义大部分被忽略(getter,setter定义),为什么我还要使用它们呢?
我有原型单元的tableview,在单元格中我有imageview和一些文本.文本标签是原型单元格中的一个,但有时它不止一行,我在服务器调用后将数据加载到表视图中.故事板行中的标签设置为0,换行符设置为Word Wrap.我也试过http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/
但没有效果.如果我在UITableView中使用自动布局用于动态单元格布局和变量行高度 一切正常,因为标签文本是预定义的,但是我从服务器加载数据并在API调用后重新加载tableView,然后标签就是一条线.
我有UITexfields我希望它只接受输入数值的其他数字警告.我希望motionSicknessTextFiled只接受数字
NSString*dogswithMotionSickness=motionSicknessTextField.text;
NSString*valueOne=cereniaTextField.text;
NSString*valueTwo=prescriptionTextField.text;
NSString*valueThree=otherMeansTextField.text;
NSString*valueFour=overtheCounterTextField.text;
Run Code Online (Sandbox Code Playgroud) 按照一些指南遇到问题,特别是 http://blog.originate.com/blog/2014/04/22/delinklinking-in-ios/
我正在设置网址方案,它可以很好地从另一个应用程序启动应用程序,但传入主机或网址似乎不应该正常工作.我正在为所有视图布局使用故事板和界面构建器.
该指南在appDelegate中显示了这个openURL:
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
if([[url host] isEqualToString:@"page"]){
if([[url path] isEqualToString:@"/page1"]){
[self.mainController pushViewController:[[Page1ViewController alloc] init] animated:YES];
}
return YES;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的版本简化,并从其他一些来源迅速,即 获取ViewController的实例从AppDelegate在Swift中我正在跳过url主机的条件,以删除问题中潜在的其他变量.
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String, annotation: AnyObject?) -> Bool {
var rootViewController = self.window!.rootViewController
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var profileViewController = mainStoryboard.instantiateViewControllerWithIdentifier("profile") as ProfileViewController
rootViewController.navigationController.popToViewController(profileViewController, animated: true)
return true
}
Run Code Online (Sandbox Code Playgroud)
swift版本导致崩溃:
fatal error: unexpectedly found nil while unwrapping an Optional value
似乎rootViewController还没有navigationController呢?
uiviewcontroller uinavigationcontroller ios appdelegate swift
这是应用程序中的一个设计问题,它使用AutoLayout,UICollectionView和UICollectionViewCell,它可以自动调整宽度和高度,具体取决于AutoLayout约束及其内容(某些文本).
它是一个UITableView列表,每个单元格都有自己的宽度和高度,每个行根据其内容单独计算.它更像是在应用程序(或WhatsUp)中构建的iOS消息.
很明显应用程序应该使用func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize.
问题是在该方法中,app不能调用func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell也不能dequeueReusableCellWithReuseIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath!) -> AnyObject实例化单元格,用特定内容填充它并计算其宽度和高度.尝试这样做会导致无限期的递归调用或其他类型的应用程序崩溃(至少在iOS 8.3中).
解决这种情况的最接近的方法似乎是将单元格的定义复制到视图层次结构中,以允许自动布局自动调整"单元格"(就像单元格具有与父集合视图相同的宽度),因此应用程序可以配置具有特定内容的单元格计算它的大小.由于资源重复,这绝对不应该是修复它的唯一方法.
所有这一切都与将UILabel.preferredMaxLayoutWidth设置为某个值相关联,该值应该是可以依赖于屏幕宽度和高度的自动布局可控(非硬编码)或至少通过自动布局约束定义设置,因此app可以获得多行UILabel内在大小计算.
我不想从XIB文件中实例化单元格,因为Storyboard应该是今天的行业标准,我希望尽可能少地干预代码.
编辑:
下面列出了无法运行的基本代码.因此,仅实例化变量cellProbe(未使用)会使应用程序崩溃.没有那个电话,应用程序运行顺利.
var onceToken: dispatch_once_t = 0
class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
dispatch_once(&onceToken) {
let cellProbe = collectionView.dequeueReusableCellWithReuseIdentifier("first", …Run Code Online (Sandbox Code Playgroud) 我正在构建我的第一个IOS应用程序,我很难找到一种方法来使用XCode6上的Swift代码来做一个简单的ScrollView,请有人帮我找到解决方案吗?
我的问题是我不知道如何在我的代码中使scrollview工作.我已经在ViewController.swift中看到了下面的代码,我希望能够在Main.storyboard中为ViewController选择Outlet"scroller",而不是我收到错误*"fatal error: Can't unwrap Optional.None (lldb)"* EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)
我有一些ViewController屏幕,其中一个我推出了一个ScrollView,我想让它使用Swift工作.
我坚持这个:
import UIKit
class ViewController: UIViewController {
@IBOutlet var scroller:UIScrollView
override func viewDidLoad() {
super.viewDidLoad()
scroller.scrollEnabled = true;
scroller.contentSize = CGSizeMake(320, 624);
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Run Code Online (Sandbox Code Playgroud)
我想如果有人可以提供一个简单的例子,如何使用swift做一个scrollview,它将解决我的问题.任何帮助都很感激.
尝试以旧样式执行此操作我尝试使用.m和.h文件执行此操作:
ViewController.m
#import "Amigo-Bridging-Header.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad]; …Run Code Online (Sandbox Code Playgroud) 我正在尝试在iOS 10和Swift 3中测试unwind segue.
我在TableViewController类中添加了segue的代码,并在表视图控制器场景中连接"取消"按钮和退出:
@IBAction func unwindToRootViewController(segue: UIStoryboardSegue) {
print("Unwind to Root View Controller")
}
Run Code Online (Sandbox Code Playgroud)
但我的简单segue不起作用.我究竟做错了什么?
我有一个小应用程序跟踪玩家在体育游戏中的上场时间.我有一个体育游戏列表,你可以点击一个并开始跟踪游戏细节.我正在尝试这样做,以便如果游戏正在进行并且用户返回到游戏列表,则他们无法点击另一个游戏单元,因为它会覆盖活动游戏中的所有当前数据.
我几乎完成了这项工作.当游戏正在进行中并且我返回到体育游戏列表时,只有活动游戏可以访问并向用户显示它是活动的.但是当我回去重置那个游戏时,我希望体育游戏的tableView都可以访问.但他们不是.它仍然只显示一个活动游戏,所有其他游戏都无法访问.我在viewWillAppear中使用tableView.reloadData.我也在下面展示了相关代码.
// gameViewController -> shows all the games you can track
override func viewWillAppear(_ animated: Bool) {
self.tableView.reloadData()
for game in fetchedResultsController.fetchedObjects! {
print("Game Opposition is \(game.opposition)")
print("Is Playing? \(game.isPlaying)")
print("Current Playing Time \(game.currentGameTime)")
print("----------------------")
}
}
// game cell view controller -> checks to see if any games are in progress
func isAnyGamesInProgress(games: [Game]) -> Bool {
let inProgressGames = games.filter({ Int($0.currentGameTime) > 0 && $0.gameComplete == false })
if inProgressGames.isEmpty {
return false
}
return true
} …Run Code Online (Sandbox Code Playgroud) ios ×9
swift ×7
height ×2
objective-c ×2
storyboard ×2
uitableview ×2
xcode ×2
appdelegate ×1
autolayout ×1
border ×1
iphone ×1
layer ×1
protocols ×1
scrollview ×1
segue ×1
swift3 ×1
uikit ×1
uitextfield ×1
unwind-segue ×1