NSNotificationCenter not Working - swift2

I want to call a method by using NSNotificationCenter .They do not call in Swift 2.0. I am newer in Swift . Please help . Any help would be apperciated.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let landingView = NewsViewController(nibName : "NewsViewController", bundle: nil)
NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
self.navigationController?.pushViewController(landingView, animated: true)
}
In my News Controller
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "methodOfReceivedNotification:", name:"NotificationIdentifier", object: nil)
}
func methodOfReceivedNotification(notification: NSNotification){
}
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self, name: "NotificationIdentifier", object: nil)
}

You called this method
NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifier", object: nil)
before your observer's created. So please post your notification after this method self.navigationController?.pushViewController(landingView, animated: true) completed. You can refer this post:
Completion handler for UINavigationController "pushViewController:animated"?
Hope that helps.

Related

Xcode8 Swift3 ImageURL returns nil

I am trying to download an image from my web server, using Xcode 8 and Swift 3. It looks like this. The request returns nil. Any suggestions why?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var view1: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func load(_ sender: UIButton) {
let url = URL(string:"http://www.example.com/folder2/pic3-1.jpg")
let task = URLSession.shared.dataTask(with: url!) { data, response,
error in
guard let data = data, error == nil else { return }
DispatchQueue.main.sync() {
self.view1.image = UIImage(data: data)
}
}
task.resume()
}
#IBAction func reset(_ sender: UIButton) {
self.view1.image="blankU.png"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
Any idea why the image is returning nil from the server?
The Xcode8, Swift3 coding is okay. The server is blocked.

Swift : EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

Hi I am brand new to swift and need some help
Every time I run my code my app crashes and spits out a " the above error
Can anyone please tell me what I need to do or what I need to replace in my code?
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
#IBOutlet weak var picker: UIPickerView!
var employeeNames = ["John","Jane","Jack","James"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.picker.delegate = self
self.picker.dataSource = self
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return employeeNames.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return employeeNames [row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print(employeeNames[row])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Your code is absolutely fine, so the only alternative is that you haven't hooked up your pickerView outlet. Ctrl click on your picker view in the storyboard and see if your outlet 'picker' shows up. If it doesn't then ctrl drag from the pickerView to your outlet to hook it up.

Same view with valid reference which becoming nil when referenced from delegate

The func yesBtSetTextTo prints not nil when called from the grey button in the pink area, but the same func print nil when is called from the yellow button. I am lost on this one.
My main veiwController.swift does not have a prepareForSegue since I did not thing it is needed here.
The func in question is at the end of this code block,
MainBtVC.swift
protocol MainBtDelegate {
func yesBtSetTextTo(name: String)
}
class MainBtVC: UIViewController, MainBtDelegate {
#IBOutlet weak var yesBt: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func yesBtSetTextTo(name: String) {
print("called")
if yesBt == nil {
print("button is nil")
} else {
print("button not nil")
}
// yesBt.setTitle("Exit", forState: UIControlState.Normal)
}
#IBAction func yesBtTapped(sender: AnyObject) {
yesBtSetTextTo("dummy")
}
TopVC.swift
class TopVC: UIViewController {
var delegate: MainBtDelegate?
override func viewDidLoad() {
super.viewDidLoad()
delegate = MainBtVC()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func buttonTapped(sender: AnyObject) {
delegate?.yesBtSetTextTo("Exit")
}

itemForActivityType not being called

i just made one wrapper class for share helper my code is as follow.
let activityVC = UIActivityViewController(activityItems: [message], applicationActivities: nil)
activityVC.setValue(subject, forKey: "subject")
activityVC.completionWithItemsHandler = {(activityType: String!, completed:Bool, objects:[AnyObject]!, error:NSError!) in
}
fromVC.presentViewController(activityVC, animated: true, completion: nil)
problem starts here, UIActivityItemSource methods not being call
override func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
switch activityType
{
case UIActivityTypeMail:
return msg
case UIActivityTypeMessage:
return msg
case UIActivityTypePostToFacebook:
return msg
case UIActivityTypePostToTwitter:
return strTwitterShare
default:
return msg
}
}
thanks for help
It should work if you use let activityVC = UIActivityViewController(activityItems: [self], applicationActivities: nil)
... then your message is provided inside your itemForActivityType method.

Type "First View Controller" does not conform to protocol "UITableViewDataSource"

So I can't seem to get this error away..
My code is:
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var tblTasks: UITableView!
//UITableViewDataSource
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
return taskMgr.tasks.count
}
//UITableViewDataSource
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "test")
cell.textLabel!.text = taskMgr.tasks[indexPath.row].name
cell.detailTextLabel!.text = taskMgr.tasks[indexPath.row].desc
return cell
}
}
I used both of the required functions for UITableViewDataSource so I'm not sure why I keep getting this error.
Try changing the method signatures as mentioned below. Notice ! is removed in the input params.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
}
Neither of each method's respective tableView parameters should be optionals
You need to implement two required methods: tableView:numberOfRowsInSection and tableView:cellForRowAtIndexPath.
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayDataSource.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "cell"
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as UITableViewCell
// information here
return cell
}
If you have a custom cell, change only de line:
var cell:MyCustomCell = self.tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as MyCustomCell
Hope It helps!

Resources