How to bind value of UILabel to an instance variable? - iphone

I am a complete newbie to mac/objective-c. My question is: I wonder if it's possible to bind a UILabel text to a variable, while not having to manually set the text when value change.
For example, on Mac OS, when I open a new Finder window and delete a file, then the global free space in the taskbar is changing. And that value also change in all the "finder" that are open.
How can I replicate such behavior using Objective-c, either on Mac or for iPhone ? I was thinking about UILabel, but I couldn't find a way different from manually set each UILabel.
thanks
Leonardo

The current version of iPhone OS (3.1) does not support bindings (such as you would find in desktop Cocoa). For the time being, you will need to write the controller glue manually to keep the UI in sync with your model.
Specifically, you would add an IBAction method in your controller, and connect the UILabel to call it when the contents changes.
This question has been covered before also:
Bindings using Interface Builder (for iPhone apps)
On the Mac, you would use Key-Value Coding (KVC) and bind the label to an object controller in IB. The bindings documentation covers this in quite some detail:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaBindings/CocoaBindings.html

You will need to research the following:
Notifications Notifications
and/or
Key Value Coding KVC
Notifications will allow you to setup up automatic notifications of changes to let's say an object (e.g. variable) who's changes you want to be cascaded throughout your program. KVC allows you to hook up data to objects and may be helpful if you're using Core Data.
I'd start with notifications first.

It is very possible and there are 2 ways to do it.
Programatically by making a UILabel in the code and simply setting the myLabel.text.text of it
Though Interface Builder where you drag and drop the UILabel where you want it and have a property in the code to hook it up to and then use that property to set the myLabel.text on it.

Related

Swift 1.2 drag and drop controls and views in Cocoa

I'm building one application where I need to make a simple window and be able to drag&drop files onto this window. In this app I have to then parse file path and write out all paths of dropped files in table or nice order (one under another). App doesn't need to take more than one file at once, but this might be a feature for future.
Up to now I have managed to make simple drag&drop in NSView (which I extended with custom class). App is correctly registering drag&drop events and I get all file path and I have also figured how to get file names out of those strings.
My question is: How can I move this "drag&drop" to NSTableView, as I wish to have file names in nicely in table?
I have tried to put tableview under NSView but it doesn't work and if I extend NSTableView class it just registers that there was drag&drop event but nothing else happens.
Do I have to extend other class or how to make this work? I'm implementing NSDraggingDestination protocol in custom class and using performDragOperation function.
I would like to have:
- whole app window can take drag&drop-ed files, not matter where I drop it it has to accept file and put file name into table
I'm sorry if this might sound trivial but I have just started learning swift and developing OS X apps and I'm not used to this new API and don't really know how all things works here.

iOS key logger implementation

I want to understand how to implement a key logger for iOS. And how can I avoid logging keys in my application, if there is iKeyMonitor (for example) installed on device?
After research I found that ikeyMonitor installs the following files on the device:
Library/Caches/.keycache
Library/MobileSubstrate/DynamicLibraries/MobileSafe.dylib
Library/MobileSubstrate/DynamicLibraries/MobileSafe.plist
Library/MobileSubstrate/DynamicLibraries/keychain.dylib
Library/MobileSubstrate/DynamicLibraries/keychain.plist
Of course it requires MobileSubstrate.
In keycache there are some HTML files that I can open in Safari with the URL localhost:8888.
In the plists there is only com.apple.springboard filter, it means that MobileSafe.dylib (and all hooks) will be applied only on
springboard app.
Even if I don't use the default keyboard view in my app for editing a UITextField, the keylogger still works. This means that Hooks are applied on UITextField.
After using class-dump for SpringBoard.app I didn't find any methods that can be related in UITextField's implementation. After using class-dump for MobileSafe.dylib I didn't find any implementation that can be substituted for UITextField either (maybe because it written on C), I think that I should analyze MobileSafe.dylib dynamically with gdb
How can the SpringBoard (that is in plist com.apple.springboard) app have an effect on UITextField class implementation? If I use method swizzling for UITextField in the dylib for com.apple.springboard will it work in other applications too?
What is the general difference between MobileLoader and MobileHooker? At what moment will changes from MobileHooker will be applied to my system?
What should I do to hook a method from UIKit (for example UITextField methods), and change its implementation for all apps on my device?
What method for analyzing iKeyMonitor can you advise me to use?
if it uses springboard in the filter then it can only affect the springboard, to hook all applications you use UIKit as the filter(more on that in 3)
My understanding of the two is that:
MobileLoader loads your code into the running application and when this happens, functions with the constructor attribute "__attribute__((constructor))" are called. In one of these constructors, MobileHooker functions are called to replace pre-existing functions/methods in the application. As for when the changes are applied, since they are changed in constructors they should be changed before any of them are actually called.
You simply use the com.apple.UIKit filter(which btw is what the keychain.dylib in iKeyMonitor uses)
class-dump does not show the hooked classes and methods of a tweak because they are not objective-c methods, instead they are functions...
The best option would be a disassembler like ida hex-rays.com/products/ida/index.shtml

How to check if a word is defined in the English dictionary in cocoa-touch?

I am trying to make a crossword app for IOS but i Don't know that how to check if a string is valid english word or not.
How can i check it.
Is there any API or online facility to check it.
Thanks in Advance
Easy to do in iOS5 using the UIReferenceLibraryViewController class' +dictionaryHasDefinitionForTerm: method.
A UIReferenceLibraryViewController object provides a dictionary
service to look up the definition of terms. You create and initialize
a reference library view controller using the initWithTerm: method.
You pass the term to define as the parameter to this method and the
definition is displayed. You can present this view controller modally
or as part of another interface. On iPad, you can set the reference
library view controller as the content view controller of a
UIPopoverController object. Optionally, use the
dictionaryHasDefinitionForTerm: class method to check if a definition
is available for a given term before creating an instance—for example,
use this method if you want to change the user interface depending on
whether a definition is available.
There is no API for this.
In order to do this, you will need to have a dictionary file (text file or database) in your application bundle. One of the faster ways to check will be to load the dictionary into memory when the application launches so you don't have to read the file for each word. This may be overkill if you simply want hardcoded crosswords, but if you are randomly generating them then this is a must.

iPhone Beginner Question - Hooking Up Actions

I'm just starting out with iPhone development, and in my hello world application I'm having a hard time hooking up a view to a controller with actions. I followed the instructions in the book I'm reading (I believe), but depending on where I run the application the app behaves differently.
-When i command-r from the code, I see only the single button I have on the view taking up the whole window. When I click the button the action is triggered.
-when I command-r from the interface designer I see the button, label, and textbox lined up correctly. When I click the button nothing happens.
Anyone know why this is happening/how to fix it? Thanks.
Launching simulator from interface builder only simulates selected .xib file. You should only do that to test your view design.
If you want to learn the basics about iPhone development I recommend:
NewBoston: http://www.thenewboston.com/?cat=34&pOpen=tutorial
Stanford: http://itunes.stanford.edu/
both are free video tutorials on how to make iPhone apps.
Good luck!
you accidentally the whole view.
Seriously. You made one wrong connection. You connected the view outlet of the viewController (aka File's Owner) with the UIButton.
just delete this connection and connect the view outlet of the File Owner with the "Root"View.
And you should probably remove almost all connections that trigger btnClicked: too. TouchUpInside from the button is enough.
And you should follow the objective-c coding style (Part II) and start all Class names with a capital letter. chapter_2ViewController should be something like Chapter2ViewController.
To my knowledge you cannot run an app from interface builder, I may be mistaken.
You should make sure your interface builder file is saved and run the app from within xcode
just a guess, but is the window size in the simulator different than that in IB? You might check the control resizing masks and on the sizing tab in IB.

Any code examples for using a UITableView to implement in-app settings?

Any code examples for using a UITableView to implement in-app settings?
That is where to get the iPhone settings app look and feel one custom builds the settings into a UITableView. So you you would custom set the sections and cells that get returned with switch type statements.
In particular interested in reviewing the coding approach for how to:
best configure the cellForRowAtIndexPath and didSelectRowAtIndexPath
how to handle those cells for which you want in cell editing (e.g. text)
those cells where you need to launch off into another screen to set a date/time for example
how to collect up parameters and pass back to calling rootViewController (in my case need to persist data out to Core Data persistence)
Note: using Core Data so not interested in using libraries such as InAppSettings [any feedback re whether such libraries might be ok appreciated at this SO question I created].
thanks
I am not sure if you can use the inappsettingskit for your needs. It is open source so you are free to modify the code as you wish, but it doesn't look as an out of the box solution.
If you want to make these settings available in the settings app you will have to live with some workarounds for example saving NSDate AND getting a nice UI control to modify it: Use a textfield, there is no control specified which let's you pick a date. Have a look at Apple's documentation.
So the last option will be coding. First of all, determine what kind of types you want to support. Then create custom TableViewCells which reflect those kinds. If some kinds do need some special way of editing, for example a color picker, you'll have to write those controllers as well. You set a cell into editing mode with the delegate method tableView:didSelectRowAtIndexPath and present the custom controller or get into editing directly for example a UITextField. A Slider is handled directly without any coding.
The cells will need some kind of identifier so you can identify those in your model. A TableView and Core Data can interact with each other pretty well by using the NSFetchedResultsController. Create a simple window based app with a template and check the Use Core Data for Storage. The rootViewController illustrates how a tableView works together with Core Data. For your case it will be getting a bit more complicated as inserting a row will have to determine what kind of cell it should add.
I think you should look into this and get back with some more specific questions.