I'm a IOS noob and I'm trying to understand how to set the datasource of of the uipicker view
i have followed This tutorial, everything seems to work correctly however i get a warning in xcode that says
'id<UIPickerViewDataSource>' from incompatible type 'StateViewController *const __strong'
it occurs on this line.
myPickerView.delegate = self;
I keep trying different things but they all lead me in the same direction. It fails.
How should i be doing this? how to i properly set the datasource of a UIpicker view.
Thanks in advance for your guidance.
You just need to tell the compiler that you're going to conform to the delegate protocol. You do that by adding <UIPickerViewDataSource> to your .h file,
#interface StateViewController : UIViewController <UIPickerViewDataSource>
You need to say
myPickerView.dataSource = self
and also
<UIPickerViewDataSource>
Step 1: Add UIPickerViewDataSource delegate in your header file
#interface StateViewController : UIViewController <UIPickerViewDataSource>
Step 2: Now add the following line in your class file
myPickerView.dataSource = self
Related
I may be in compiler hell right here.
I'm implementing a Snapshot test in Swift, calling a property on an Objective-C VC, but that property is a class, written in Swift, bridged in.
In MyViewController.h:
#class TextEntryView;
#interface MyViewController: AbstractTextEntryViewController
#property (nonatomic, strong) TextEntryView *textEntryView;
#end
In TextEntryView.swift:
#objc(TextEntryView) class TextEntryView: UIView
And in my test, I'm trying to call
vc.textEntryView where vc is of type MyViewController and I'm getting the error:
value of type MyViewController has no member textEntryView
My bridging headers look good. If I add an NSString property to the .h file above, I'm able to reference it in the test. Also, when I command-click on MyViewController in my test, it takes me to the .h file rather than the .swift generated version of that file (which seems like a symptom of this problem).
I may be pushing Xcode 8 beyond its limits.
You need to make sure you import your app at the top of the source file for your test. For example, if your app is called MyApp, insert at the top of the test file:
import MyApp
If I leave out the import, I get the same behavior you are seeing. Additionally, as long as the import is there, you shouldn't have to bother with bridging headers for the unit test.
Have you tried to import to Test Target ?
Since you already imported the Xcode-generated header file for your Swift code into Objective-C .m file.
Please also remove #objc annotation from TextEntryView class since it's a subclass of UIView thus accessible and usable in Objective-C. keeping the annotation #objc may cause a side effect.
To be accessible and usable in Objective-C, a Swift class must be a
descendant of an Objective-C class or it must be marked #objc.
a simple case of "side-effect" is when a (swift) UIViewController subclass is marked #objc and used as custom subclass in storyBoard:
instantiateViewControllerWithIdentifier will instantiate a UViewController instead of the subclass we set in the storyBoard.
with error Unknown class _TtC10AbcdViewController in Interface Builder file
I have a warning in Xcode Assigning to ... from incompatible type. It appears when i set delegate of tableView to UIViewController without UITableViewDelegate protocol.
This warnings are annoying and i want to disable it. However i can't find how to do this. Only this post showed something similar.
How to disable this warning?
What is the way to find out how to disable different types of warnings? In case there would be another warning, that i want to disable.
So there are some downvotes from a couple crybabies, but the answer for this question can be useful in some cases. Maybe not in this one but at similar situations.
I finally found the answer and it seems, that the only way now (end 2016) to implement protocol and than use
#pragma GCC diagnostic ignored "-Wprotocol"
This warning appears when your ViewController does not confirm with UITableViewDelegate protocol in .h file.
To resolve this:
In ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDelegate>
#property NSString *myString;
#end
Hope this helps.
I have to use a library for my iOS project.
the library offers the class ICDevice (abstract) with a delegate ICDeviceDelegate.
the ICDeviceDelegate offers the following funtion
-(void)accessoryDidConnect:(ICISMPDevice *)sender;
I want to integrate this function in my ViewController to detect when the device is connected. How to do that?
Go to your .h and set your controller as delegate of the library #interface YourViewController : UIViewController <LibraryDelegate> so then you will be able to set your view controller as delegate when creating the library object
YourLibrary *library = [YourLibrary new];
library.delegate = self;
Delegates are a very important pattern in Cocoa/CocoaTouch, I suggest you read Apple's documentation to get a good grasp on it.
Once you understand them properly, it should be trivial to figure out the answer to your question.
Documentation
I'm a huge noob, let's get that straight.
Currently I have in my header file...
#interface ViewController : UIViewController <UIWebViewDelegate>
...but can I have multiple delegates in the h/m files?
Can I add somehow?
The reason I ask this, is because of a warning I'm getting...
"Assigning to 'id' from incompatible type 'ViewController *const __strong'"
The app works fine, but I want to make sure the code is 100% proper.
(LONG STORY SHORT, I'm trying to add location tracking to my app, and I did, and it works fine, but it's giving me a warning because I'm 100% positive I didn't implement it properly, as I am trying to have multiple "XXX.delegate = self;" going on.
I want to somehow have...
self.locationManager.delegate = self;
AND
tapView.delegate = self;
in the same "-(void)viewDidLoad"...
Again, I expect people to literally say "Wtf are you smoking, you are doing this all wrong", because I am.. I need help. Please help. I've been googling all day.
That was a very long winded way of asking how to do this:
UIViewController <UIWebViewDelegate, UITextFieldDelegate, UITableViewDelegate>
There is no issue in doing this at all. You must specify the protocol to remove the warning
You can confirm multiplay delegates i.e.
#interface ViewController : UIViewController <UIWebViewDelegate,SomeOtherDelegate>
Well, you can conform to multiple protocols in your .h file like that:
#interface ViewController : UIViewController <UIWebViewDelegate, UITableViewDelegate, UITableViewDataSource>
Ad you can also conform to more protocols in your .m file with a private class extension like that:
#interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
Don't mix them up
in .h file
#interface YourControllerClass : UIViewController <UIAlertViewDelegate, UIWebViewDelegate UITextFieldDelegate>
You can add Delegates when ever you need them. These Delegates have their methods defined. You can use their methods as you wish
I am trying to add pull to refresh to my app and am using the Pull to Refresh files here: https://github.com/leah/PullToRefresh However I am confused on how to make a subclass of my table view. How does one do that?
You mean something like this?
#interface MyTableViewController : PullRefreshTableViewController
{
//Member variables here.
}
// Methods and protocols here.
#end