我在视图控制器的顶部添加了一个导航栏.我试图根据条件控制按钮是否可见,但是我在添加按钮时遇到问题.到目前为止,我有,
var addButton: UIBarButtonItem = UIBarButtonItem(title: "test", style: .done, target: self, action: #selector(addTapped))
override func viewDidLoad() {
super.viewDidLoad()
let boool = true
if boool {
self.navigationItem.rightBarButtonItem = self.addButton
}
else {
self.navigationItem.rightBarButtonItem = nil
}
}
func addTapped(sender: AnyObject) {
print("hjxdbsdhjbv")
}
Run Code Online (Sandbox Code Playgroud)
我认为它没有正常工作,因为我已经在VC中添加了导航栏,而不是使用导航控制器并在那里使用栏.我想知道是否有办法使用此导航栏.
我希望能够向应用程序用户询问他/她当前的位置以及在该位置自动删除的图钉.这是我获取当前位置的代码,但我无法理解如何删除当前位置的引脚.
import UIKit
import MapKit
import CoreLocation
class MapVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// User's location
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
if #available(iOS 8.0, *) {
locationManager.requestAlwaysAuthorization()
} else {
// Fallback on earlier versions
}
locationManager.startUpdatingLocation()
// add gesture recognizer
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(MapVC.mapLongPress(_:))) // colon needs to pass through info
longPress.minimumPressDuration = 1.5 // in seconds
//add gesture recognition
map.addGestureRecognizer(longPress) …Run Code Online (Sandbox Code Playgroud) 我希望能够MKLocationSearchCompletion array在用户搜索搜索栏时找到个人位置.但是,我无法理解项目如何存储到对象中以及是否可以将地标对象(或位置信息)添加到MKLocationSearch对象中.我从文档中获得的是MKLocalSearchCompleter对象存储当用户在搜索栏中键入部分字符串时访问的字符串.但我不知道在哪里可以访问此阵列并添加新位置.
以下是代码的结构,以显示搜索完成结果:
var searchCompleter = MKLocalSearchCompleter()
var searchResults = [MKLocalSearchCompletion]()
@IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
searchCompleter.delegate = self
searchBar.delegate = self
}
extension ViewController: MKLocalSearchCompleterDelegate {
func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
searchResults = completer.results
searchResultsTableView.reloadData()
}
func completer(_ completer: MKLocalSearchCompleter, didFailWithError error: Error) {
// handle error
}
}
extension ViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: …Run Code Online (Sandbox Code Playgroud) 自从我更新到Xcode 9和Swift 4.0后,我的模拟器中的mapView运行速度非常慢.我认为这是因为代码会被弃用或因为我的代码很广泛而导致速度变慢,但是我将mapView添加到空白项目中,并且mapView导航/缩放的速度也很慢!我需要配置一些设置来在模拟器中修复此问题吗?
这是控制台日志中生成的输出:
2017-10-26 23:25:50.932530-0500 mapTest[7994:151068] [VKDefault] Tile 1.2.2 (256) in current unloaded state for 0.07 seconds - Polygons - Loading (ephemeral) (0.07 sec), Rivers - Loading (ephemeral) (0.07 sec), Roads - Loading (ephemeral) (0.07 sec), Point Labels - Loading (ephemeral) (0.07 sec), Polygon Labels - Loading (ephemeral) (0.07 sec), Road Labels - Loading (ephemeral) (0.07 sec), Resources - Unavailable (terminal) (2.11 sec), Attributes - Loading (ephemeral) (0.07 sec)
Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,该应用程序可以在用户当前位置放置图钉,并根据用户输入的位置来放置图钉。当前位置注释放置得很好,但是当尝试放置用户的输入坐标时,注释未放置。
我已经正确添加了注释,map.addAnnotation(annotation)其中 annotation.coordinate (for New York) is CLLocationCoordinate2D(latitude: 40.713054, longitude: -74.007227999999998)。
我发现该viewForAnnotation方法没有被调用,我认为这就是没有放置注释的原因(打印注释不会产生任何结果)。我有一个请求输入坐标的测试应用程序,它可以放置注释。同样在测试应用程序中,将 viewForPrints 中的注释打印出一个值。
下面我粘贴了一些我认为与该问题相关的代码。如果您需要更多请评论。
import UIKit
import MapKit
import CoreLocation
class MapVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
map.delegate = self
}
Run Code Online (Sandbox Code Playgroud)
下面的这个函数从另一个类获取值并将坐标转换为 CLLocationCooperative2D 类型。如上所述,注释.坐标(对于纽约)产生CLLocationCoordinate2D(latitude: 40.713054, longitude: -74.007227999999998),因此 add 可以保存坐标。
func add(newLocation location_one:[String:Any]) {
let momentaryLat = (location_one["latitude"] as! NSString).doubleValue
let momentaryLong = (location_one["longitude"] as! NSString).doubleValue
let annotation = MKPointAnnotation()
annotation.title = location_one["title"] as? …Run Code Online (Sandbox Code Playgroud) 如标题中所述,我有兴趣在搜索栏外单击时从搜索栏中取消键盘.类似的问题在touchesBegan中使用了self.view.endEditing(true),但我没有运气.我不确定我是否遗漏了其他任何东西,或者我是否正确设置了我的搜索栏.
import UIKit
import MapKit
class AddressVC: UIViewController {
@IBOutlet weak var searchBar: UISearchBar!
var searchCompleter = MKLocalSearchCompleter()
var searchResults = [MKLocalSearchCompletion]()
@IBOutlet weak var searchResultsTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
// Do any additional setup after loading the view, typically from a nib.
searchResultsTableView.isHidden = true
searchCompleter.delegate = self
}
extension AddressVC: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchCompleter.queryFragment = searchText
}
func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
self.searchBar.endEditing(true)
searchBar.resignFirstResponder() …Run Code Online (Sandbox Code Playgroud) 我在tableview单元格上设置了一个按钮,我想知道如何操作它.也就是说,我有兴趣更改按钮的名称标题,并在单击按钮时添加另一个目标(按钮的不同功能).我的想法是需要将它添加到buttonClicked func中,但我不确定如何引用被单击的特定cellForRow.或许可以在cellForRow中使用条件来确定按钮标题是什么?我不太清楚最好的办法是什么.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = guestTableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let button : UIButton = UIButton(type:UIButtonType.custom) as UIButton
button.frame = CGRect(origin: CGPoint(x: 200,y :60), size: CGSize(width: 100, height: 24))
let cellHeight: CGFloat = 44.0
button.center = CGPoint(x: view.bounds.width / (4/3), y: cellHeight / 2.0)
button.setTitleColor(.blue, for: .normal)
button.addTarget(self, action: #selector(buttonClicked), for: UIControlEvents.touchUpInside)
button.setTitle("Add", for: UIControlState.normal)
cell.addSubview(button)
return cell
}
func buttonClicked(sender : UIButton!) {
print("Added!")
}
Run Code Online (Sandbox Code Playgroud) swift ×7
mkmapview ×3
ios ×2
annotations ×1
arrays ×1
button ×1
object ×1
simulator ×1
uisearchbar ×1
uitableview ×1